2

I want to know why the following code prints:

Beta 44
44 44

Please correct me because my analysis is apparently not correct but I cannot figure out why.

b.h gets called first, and it is 44 then b.getH() is called and it prints "Beta 44". So the console prints 44 Beta 44. Why is it not the case?

public class Beta {

    public int h = 44;
    public int getH(){
        System.out.println("Beta "+h);
        return h;
    }
    public static void main(String[] args) {
        Beta b = new Beta();
        System.out.println(b.h+" "+ b.getH());
    }

}
OPK
  • 4,120
  • 6
  • 36
  • 66

5 Answers5

2
System.out.println(b.h+" "+ b.getH());

is equivalent to (YMMV)

StringBuilder builder = new StringBuilder();
String result = builder.append(b.h).append(" ").append(b.getH()).toString();
System.out.println(result);

As you can see, though b.h is evaluated first, it is not printed until the whole String value is constructed from the concatenation. That construction involves invoking b.get() which has its own

System.out.println("Beta "+h);
Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
2

To run the line System.out.println(b.h+" "+ b.getH()); in main, it is necessary to call b.getH first, and then System.out.println.

The first line of your output comes from the call to b.getH (in particular, from the System.out.println inside that method). The second line comes from the enclosing call to System.out.println.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
2

Java must evaluate all necessary arguments before operators perform their operation.

In this case, b.getH() is called before b.h + " " gets concatenated to the result of b.getH().

Here,

  1. b.h is concatenated to " " to yield "44 ".
  2. b.getH() is called.
  3. getH() prints "Beta 44".
  4. getH() returns 44.
  5. 44 gets concatenated to the end of "44 " from step (1), yielding "44 44", which is now printed.
rgettman
  • 176,041
  • 30
  • 275
  • 357
2

b.h gets called first

b.h is a field, not a method, so it cannot be "called". Your code reads the value from it, gets 44, and saves it for the time being; nothing gets printed yet.

then b.getH() is called and it prints "Beta 44"

That's right - this is how you get the first line, i.e. "Beta 44"

So the console prints 44 Beta 44

That is not what the console should print, because "Beta 44" from inside getH() must finish printing before b.h+" "+ b.getH() starts to print.

The console cannot start printing b.h+" "+ b.getH() before getting both parts. Once b.getH() returns, println has all the parts ready, i.e. 44 obtained from reading b.h, the space to put in between, and finally the 44 to print at the end. Hence, "44 44" gets printed on the second line.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

First b.h+" "+ b.getH() has to evaluate before it can be printed. Based on your expected output, you could have done something like

public static void main(String[] args) {
    Beta b = new Beta();
    System.out.print(b.h);
    System.out.print(" ");
    System.out.println(b.getH());
}

Your existing version is equivalent to

String temp = b.h+" "+ b.getH();
System.out.println(temp);
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249