0

As I have read online that Java is pass by value and a general swap function won't swap the two values. I also read that it's not possible to swap the values of primitive types. I am wondering why the following program works and displays different valies after swap ?

public class swapMe { public static void main(String[] args) { int x = 10 , y = 20; System.out.println("Before"); System.out.println("First number = " + x); System.out.println("Second number = " + y); int temp = x; x = y; y = temp; System.out.println("After"); System.out.println("First number = " + x); System.out.println("Second number = " + y); } } 

Is it like somewhere, the original values of x = 10 and y = 20 are still stored somewhere and the swapped values displayed are not correct? Please advise. Thanks

3
  • 2
    (facepalm!) are you passing those values to any method, in order to check the swap function won't swap theory??? Commented Sep 11, 2017 at 14:58
  • Value of x stored in temp after int temp = x; executes. And so on. They aren't "somewhere", you yourself told compiler exactly where they should be. Commented Sep 11, 2017 at 15:00
  • You need to understand the difference between literal swapping and exchanging + assigning values between variables. Additionally - "As I have read online that Java is pass by value" you never passed any variables to any methods in your example Commented Sep 11, 2017 at 15:00

1 Answer 1

3

Not entirely sure where you're getting that information, but let's take this one at a time.

As I have read online that Java is pass by value and a general swap function won't swap the two values.

Correct...if the expectation of the swap is to happen by virtue of calling a method.

 public void swap(int x, int y) { int tmp = x; x = y; y = tmp; } // meanwhile, in main int x = 10; int y = 20; swap(x, y); System.out.println(x); // still prints 10 System.out.println(y); // still prints 20 

Incorrect...if the swap happens inside the method and is utilized somehow.

 public void swap(int x, int y) { int tmp = x; x = y; y = tmp; System.out.println(x); // will print 20 from main System.out.println(y); // will print 10 from main } // meanwhile, in main int x = 10; int y = 20; swap(x, y); System.out.println(x); // still prints 10 System.out.println(y); // still prints 20 

I also read that it's not possible to swap the values of primitive types.

No, this is perfectly possible to do. You can always reassign variables.

As to why your example above works, it's because you're holding onto one of the values while you reassign one of its variables. You're basically putting one value to the side while you copy it over.

Slowly...

int x = 10; int y = 20; int tmp = x; // tmp = 10 x = y; // x = 20, tmp = 10 y = tmp; x = 20, y = 10; tmp = 10 (but that doesn't matter) 
Sign up to request clarification or add additional context in comments.

4 Comments

You technically can't "swap" values between primitives: 4 will stay 4 whatever you do with it. OP just confuses swapping and assignments.
@M.Prokhorov: It isn't like Python's style of swapping in which you don't need an intermediate variable, but reassignment is congruent to swapping when used in this context.
I'm pretty sure Python also uses third variable, it just won't show that to you.
@M.Prokhorov: I did say you don't need it. Python likely needs it, but you don't need to invoke it. x, y = y, x is the most common form of that for defined values of x and y.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.