This happened when I solved the question "Cracking the Coding interview":
Write a function to replace the number in place (i.e. without temporary variables)
I decided to write my solution in Java (because I plan to use Java in my interviews for internship.)
I came up with a solution in which I was almost sure was the correct answer (because I did it on one line):
public static void main(String args[]) { int a = 5; int b = 7; a = b - a + (b = a); System.out.println("a: " + a + " b: " + b); }
Of course, this code fulfills the desired result. a == 7 and b == 5 .
Now here is the fun part.
This code will not work in C ++ and this solution will not be at the end of the book.
So my question is: why exactly does my solution work? I assume Java does something different than other languages?
source share