• Log in or sign up for an account here: https://www.sythe.org/login

Java Problem/Puzzle

Pottworth

Member
Joined
Nov 27, 2005
Sythe Gold
0
Messages
83
Location
UK
Okay, so I was in a lecture last week, and my teacher was talking about a question she got asked on the spot in a job interview. It's been bugging me ever since. It was something along the lines of;

Say you had two integer variables. How would you switch the values WITHOUT creating a temporary variable?

Can anyone figure out the solution? It's got something to do with arithmetic I believe.
 
You're right about it being arithmetic. You can do something like this.

Code:
[FONT=monospace]
[/FONT]variableOne = [FONT=monospace][/FONT]variableOne + variableTwo;[FONT=monospace]
[/FONT]variableTwo = [FONT=monospace][/FONT]variableOne - variableTwo;
[FONT=monospace][/FONT]variableOne = [FONT=monospace][/FONT]variableOne [FONT=monospace]-[/FONT] variableTwo;
 
Thanks for the replies! I'm kicking myself I didn't figure out the first method. I'd have never guessed the second.
 
duelarena.gg
Back
Top