prefix and postfix increments while comparing variables

Posted by miatech on Stack Overflow See other posts from Stack Overflow or by miatech
Published on 2012-11-03T16:53:59Z Indexed on 2012/11/03 17:00 UTC
Read the original article Hit count: 179

Filed under:
|
|
|

could someone explain why this code output is

not equals
not equals 2

in the first if statement it seems that a = 0 b/c is a postfix increment; therefore a will not increase untile next line; however, the two a's are not equal why? and in the second if when I run the debugger the value of a is 2, but the test is false, why?

public static void main (String[] args)
   {
       int a = 0;
       if (a++ == a++) {
           System.out.println("equals");
       } else {
           System.out.println("not equals");
       }

       if (++a == 2) {
           System.out.println("equals 2");
       } else {
           System.out.println("not equals 2");
       }

   } 

© Stack Overflow or respective owner

Related posts about java

Related posts about if-statement