Reputation: 111
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
Actual output
c c 1 2
Why?
Upvotes: 9
Views: 435
Reputation: 1508
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.
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. after the second call to new W() the Statement will look like.
System.out.println(1 +""+2);
and the output will be :- c c 1 2
Upvotes: 0
Reputation: 2351
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:
1st W object is instantiated and its count property read.
Here the first c is send to output.
2nd W object is instantiated and its count property read.
Here the second c is send to output.
The string argument for System.out.println() is built. ("1 2")
The string argument is sent to the output.
Thus the output results to c c 1 2.
Upvotes: 0
Reputation: 620
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. Finally, you call system.out.println(1 + " " + 2). then the 1 2 is in the output buffer.
Upvotes: 0
Reputation: 74036
The actual order of things executed by the JVM is as follows:
1st W
object is instantiated and its count
property read.
Here the first c
is send to output.
2nd W
object is instantiated and its count
property read.
Here the second c
is send to output.
The string argument for System.out.println()
is built. ( == "1 2"
)
The string argument is sent to the output.
Thus the output results to c c 1 2
.
Upvotes: 7
Reputation: 1283
the println method in the main needs to know what is its's parameters, so the JVM first constructs the objects and then send them to the println method
Upvotes: 0
Reputation: 94429
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.
The order of operations is:
Upvotes: 7
Reputation: 13556
This is because the System.out.println
statement in the constructor is executed first and then only the calling System.out.println
executed. Remember the System.out.pritnln
in constructor executed completely before the calling System.out.println
The order is
System.out.println()
to be called hence printing "C"System.out.println()
to be called hence printing "C"System.out.println
prints new count.Upvotes: 0