KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I want to swap the value of two integer variables in java using the XOR operator. This is my code: int i = 24; int j = 17; i ^= j; j ^= i; i ^= j; System.out.println("i : " + i + "\t j : " + j); It will work fine but the following equivalent code doesn't work: int i = 24; int j = 17; i ^= j ^= i ^= j; System.out.println("i : " + i + "\t j : " + j); Output is like this: i : 0 j : 24 First variable is zero! What's wrong with Java?
Tags (comma-separated)
Save Edits
Cancel