[英]Weird behavior of println() method in Java
class W {
static int count=0;
W() {
count++;
System.out.print("c ");
}
public static void main(String[] args) {
System.out.println(new W().count+" "+new W().count);
}
}
Expected output 预期产出
c 1 c 2
c 1 c 2
Actual output 实际输出
cc 1 2
cc 1 2
Why? 为什么?
In this line: 在这一行:
System.out.println(new W().count+" "+new W().count);
The instances of W
are instantiated prior to the evaluation of the rest of the statement. W
的实例在评估其余语句之前被实例化。
The order of operations is: 操作顺序是:
The actual order of things executed by the JVM is as follows: JVM执行的实际顺序如下:
1st W
object is instantiated and its count
property read. 实例化第一个
W
对象并读取其count
属性。
Here the first c
is send to output. 这里第一个
c
发送到输出。
2nd W
object is instantiated and its count
property read. 实例化第二个
W
对象并读取其count
属性。
Here the second c
is send to output. 这里第二个
c
发送到输出。
The string argument for System.out.println()
is built. 构建
System.out.println()
的字符串参数。 ( == "1 2"
) (==
"1 2"
)
The string argument is sent to the output. 字符串参数将发送到输出。
Thus the output results to cc 1 2
. 因此输出结果为
cc 1 2
。
This is because the System.out.println
statement in the constructor is executed first and then only the calling System.out.println
executed. 这是因为构造函数中的
System.out.println
语句首先执行,然后只执行调用System.out.println
。 Remember the System.out.pritnln
in constructor executed completely before the calling System.out.println
The order is 记住构造函数中的
System.out.pritnln
在调用System.out.println
之前完全执行顺序是
System.out.println()
to be called hence printing "C" System.out.println()
因此打印“C” System.out.println()
to be called hence printing "C" System.out.println()
因此打印“C” System.out.println
prints new count. System.out.println
打印新计数。 main中的println方法需要知道它的参数是什么,所以JVM首先构造对象然后将它们发送到println方法
class W {
static int count=0;
W() {
count++;
System.out.print("c ");
}
public static void main(String[] args) {
System.out.println(new W().count+" "+new W().count);
}
Because new W() will call the constructor W() then it will print "C" out, after that the count become 1 then you call new W() again, so another "C" is here. 因为新的W()将调用构造函数W()然后它将打印出“C”,之后计数变为1然后再次调用新的W() ,所以另一个“C”就在这里。 Finally, you call system.out.println(1 + " " + 2).
最后,调用system.out.println(1 +“”+ 2)。 then the 1 2 is in the output buffer.
然后1 2在输出缓冲区中。
class W {
static int count=0;
W() {
count++;
System.out.print("c ");
}
public static void main(String[] args) {
//modify System.out.println(new W().count+" "+new W().count);
System.out.print(new W().count + " ");
System.out.print(new W().count);
}
}
OUTPUT
c 1 c 2
The actual order of things executed by the JVM is as follows: JVM执行的实际顺序如下:
1st W object is instantiated and its count property read. 实例化第一个W对象并读取其count属性。
Here the first c is send to output. 这里第一个c发送到输出。
2nd W object is instantiated and its count property read. 实例化第二个W对象并读取其count属性。
Here the second c is send to output. 这里第二个c发送到输出。
The string argument for System.out.println() is built. 构建System.out.println()的字符串参数。 ("1 2")
(“1 2”)
The string argument is sent to the output. 字符串参数将发送到输出。
Thus the output results to cc 1 2. 因此输出结果为cc 1 2。
class W {
static int count=0;
W() {
count++;
System.out.print("c ");
}
public static void main(String[] args) {
System.out.println(new W().count+" "+new W().count);
}
}
this is to do with the operator priority in java. 这与java中的运算符优先级有关。
The new operator has higher precedence over the concatenation that's why the second new operator gets priority over the concatenation operator and the second c is getting print. 新的运算符优先于连接,这就是为什么第二个新运算符优先于连接运算符而第二个c正在进行打印的原因。 after the second call to new W() the Statement will look like.
在第二次调用新的W()之后,Statement将会是这样的。
System.out.println(1 +""+2);
and the output will be :- cc 1 2 输出将是: - cc 1 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.