How to Swap Two Numbers in Java


In this example we will show how to swap value of two numbers in Java.

Source Code

package com.beginner.examples;

public class SwapNumberExample {

    public static void main(String[] args){
    	int m = 2019;
    	int n = 10;
    	//temporary variable
		int tmp  = n;
		//swap the value
		n = m;		
		m = tmp;	
		System.out.println("m = " + m + " and n = " + n);
	}
}

Output:

m = 10 and n = 2019
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments