Java Code To Check Swap Two Numbers [with Temp Variable]

March 7, 2023

Program

public class SwapWith 
{
	public static void main(String[] args) 
	{
		int a = 10;
		int b = 8;
		int temp;
		temp = b;
		b = a;
		a = temp;
		System.out.println(a);
		System.out.println(b);
	}
}

Compile:

javac SwapWith.java
java SwapWith

Output:

8
10

You Might Also Like