Tuesday, February 12, 2013

SCJP Question: postfix increment

What is the output of this code?

class A {
  int i=0;

  public int getValue(){
    return i++;
  }
}

class Test {
  public static void main ( String args [ ] ) {
    System.out.println(new A().getValue());
  }
}




Answer:
The output is 0.
First the value is returned and only then it is incremented and stored.

No comments:

Post a Comment