The ambiguities are those issues that are not defined clearly in the Java language specification. The different results produced by different compilers on several example programs support our observations. So in this article let’s discuss swap ambiguity in Java.
Problem statement
Why when a Wrapper Class object is passed in a function call then function values don’t swap? How to swap it?
Simple Swap in Java using Primitive Data Type
Swapping the Values Using the Third Variable
Algorithm:
swap() function can be implemented using the following steps:
- Store the value of variable a in a temporary variable
- Store value of b in a
- Store value of temp in b
Java
/*package whatever //do not write package name here */ import java.io.*; class GFG { public static void main(String[] args) { int a = 10 ; int b = 20 ; System.out.println( "Before swapping a=" + a + " and b=" + b); swap(a, b); System.out.println( "After swapping a=" + a + " and b=" + b); } private static void swap( int a, int b) { int temp = a; a = b; b = temp; } } |
Output:
Before swapping a=10 and b=20 After swapping a=10 and b=20
This doesn’t swap because in java functions are always a call by value because variables a and b are just copies of the actual parameters their scope is with the function only.
Swapping using Wrapper Class
Swapping the Values Using the Third Variable
Algorithm:
swap() function can be implemented using the following steps:
- Store the value of variable a in a temporary variable
- Store value of b in a
- Store value of temp in b
Java
/*package whatever //do not write package name here */ import java.io.*; class GFG { public static void main(String[] args) { Integer a = 10 ; Integer b = 20 ; System.out.println( "Before swapping a=" + a + " and b=" + b); swap(a, b); System.out.println( "After swapping a=" + a + " and b=" + b); } private static void swap(Integer a, Integer b) { Integer temp = a; a = b; b = temp; } } |
Output:
Before swapping a=10 and b=20 After swapping a=10 and b=20
This also doesn’t swap though Wrapper classes are objects because wrapper classes are declared final. A field can be declared as final. Doing so prevents its contents from being modified, making it, essentially, a constant. This means that you must initialize a final field when it is declared.
Then how to swap?
Swap using Arrays
This issue can be solved using Arrays. Swapping the Values Using the Third Variable.
Algorithm:
swap() function can be implemented using the following steps:
- Declare two Arrays of size 1
- Store the value of array a in a temporary variable
- Store the value of array b in a
- Store the value of temp in array b
Java
/*package whatever //do not write package name here */ import java.io.*; class GFG { public static void main(String[] args) { int [] a = { 10 }; int [] b = { 20 }; System.out.println( "Before swapping a=" + a[ 0 ] + " and b=" + b[ 0 ]); swap(a, b); System.out.println( "After swapping a=" + a[ 0 ] + " and b=" + b[ 0 ]); } private static void swap( int [] a, int [] b) { int temp = a[ 0 ]; a[ 0 ] = b[ 0 ]; b[ 0 ] = temp; } } |
Output:
Before swapping a=10 and b=20 After swapping a=20 and b=10
This swaps because we pass the reference of the array in the function.
- Time complexity: 0(1)
- Space complexity: O(1)