Jatin Tamboli
Jatin Tamboli

Reputation: 111

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

Actual output

c c 1 2

Why?

Upvotes: 9

Views: 435

Answers (7)

kTiwari
kTiwari

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

sunysen
sunysen

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

YaleCheung
YaleCheung

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

Sirko
Sirko

Reputation: 74036

The actual order of things executed by the JVM is as follows:

  1. 1st W object is instantiated and its count property read.

    Here the first c is send to output.

  2. 2nd W object is instantiated and its count property read.

    Here the second c is send to output.

  3. The string argument for System.out.println() is built. ( == "1 2")

  4. The string argument is sent to the output.

Thus the output results to c c 1 2.

Upvotes: 7

ReZa
ReZa

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

Kevin Bowersox
Kevin Bowersox

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:

  1. Instantiate first new W(), causing c to be printed
  2. Evaluate first new W().count
  3. Instantiate second new W(), causing c to be printed
  4. Evaluate second new W().count
  5. Concatenate the result and print.

Upvotes: 7

Prasad Kharkar
Prasad Kharkar

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

  • new W() causes System.out.println() to be called hence printing "C"
  • again new W() causes System.out.println() to be called hence printing "C"
  • After this is complete, outer System.out.println prints new count.

Upvotes: 0

Related Questions