Java Puzzle operator confusion

Why does this
int _=1, __=2;
int j = __ ^_^ __, k = __-- -+- _ -+- --__;
System.out.println(j + " " + k);
print the following?
1 3

Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. right expressions in Java are evaluated from left to right (my mistake) so let's think like Java now:

    i = 2
    j = 1
    i-- + j = 3
    i-- + j + --i = 3 (i is already evaluated and equals 1 so --i = 0)

    right?

    ReplyDelete
  3. Brillant, you made it look easy. :)

    ReplyDelete
  4. to compare:
    Java 1 3
    C/C++ 1 4
    Python 1 5
    Perl 1 3

    ReplyDelete
  5. @Przemyslaw, Thank you for the comparison. ;)

    ReplyDelete

Post a Comment

Popular posts from this blog

Java is Very Fast, If You Don’t Create Many Objects

Low Latency Microservices, A Retrospective

Unusual Java: StackTrace Extends Throwable