Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print's order of execution

This program gives output -

A 1 2

Shouldn't it give output -

1 A 2

since first a.i should print 1 and then a.getI() executes and should print A 2

public class A1{

     int i=1;

     public int getI(){
          System.out.print("A ");
          return i+1;
     }

     public static void main(String args[]){
          A1 a=new A1();
          System.out.print(a.i+" "+a.getI());
     }
}
like image 908
Naman Avatar asked Apr 28 '12 13:04

Naman


1 Answers

In this expression:

a.i+" "+a.getI()

The call to a.getI() gets evaluated first, and afterwards the string is formed by concatenating a.i plus a.getI()

like image 157
Óscar López Avatar answered Oct 12 '22 03:10

Óscar López