posted 19 years ago
Fred, Garrett - that's not really going to help, is it? Not given they way the question has been asked.
wrushasen - in Java there is no way to do this. Not if a and b are local variables, and you're trying to swap them within a separate method. That's because the variables inside the swap method just get copies of a and b. Nothing you do inside swap will have any effect on the values a and b outside the swap method. The discussion Nitesh linked to is accurate.
If you want to do something like this, then you need to put the data into something besides local primitive variables. A common choice would be to put numbers in an array, then write a method to swap two elements (element i and j) of the array. E.g.
Output: 3 2 1 4
Try writing a swap() method that will swap two elements of the array, as shown.
"I'm not back." - Bill Harding, Twister