Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaJava Swap Ambiguity

Java Swap Ambiguity

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:

  1. Store the value of variable a in a temporary variable
  2. Store value of b in a
  3. 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:

  1. Store the value of variable a in a temporary variable
  2. Store value of b in a
  3. 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.

Integer class is a final class hence cannot be changed

Integer class is a final class hence cannot be changed

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:

  1. Declare two Arrays of size 1
  2. Store the value of  array a  in a temporary variable
  3. Store the value of array b in a
  4. 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)
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments