Java中System.out对象的print方法与println方法有什么区别?(除了前者打印换行、后者不打印换行外)

2024-11-20 14:36:19
推荐回答(4个)
回答1:

看一下java api
----------------------------------------------------------------------------------------
public void print(char[] s)

Prints an array of characters. The characters are converted into bytes
according to the platform's default character encoding, and these bytes are
written in exactly the manner of the write(int)
method.

Parameters:
s - The array of chars to be printed
Throws:
NullPointerException
- If s is null
----------------------------------------------------------------------------------------
println
public void println(char[] x)

Prints an array of characters and then terminate the line. This method
behaves as though it invokes print(char[])
and then println().

Parameters:
x - an array of chars to print.
----------------------------------------------------------------------------------------

一目了然,
println()不会抛出任何异常,即使是空的。
print()如果是空的,会抛出NullPointerException异常。

回答2:

一定是你的代码哪里有问题,下面是我的代码,打印正常
import org.junit.Test;

public class My {

@Test
public void run() {
InstanceSet set = new InstanceSet(5);
System.out.println(set.getNode(2));
System.out.print(set.getNode(2));
}

public class Node {

}

public class InstanceSet {
private Node[] cycle;

public InstanceSet(int sum) {
cycle = new Node[sum];
for (int i = 0; i < sum; i++)
cycle[i] = new Node();
}

public Node getNode(int index) throws ArrayIndexOutOfBoundsException {
return cycle[index];
}
}
}

回答3:

就这区别 似乎 --没其他区别

回答4:

有Node类的代码吗?你的QQ是多少?我直接联系你。