Home » Java programs

Java program to swap two integer numbers using class

Submitted by BalRam Dixit, on May 19, 2017
Learn: How to read and swap two integer numbers in Java language using class, that means swapping of numbers operations will be performed using class.

In this Java program, we are going to read two integer numbers and swap them, program is implemented using class that means, there will be methods to read integer numbers, to display numbers and two swap the numbers.

Note: This program is not using temporary variable to swap the numbers.

Consider the program

import java.util.Scanner;

public class Swap{
	private int num1;
	private int num2;
	public void setNum1(int num){
	public int getNum1(){
		return this.num1;
	public void setNum2(int num){
	public int getNum2(){
		return this.num2;
	public void swapNumber(){
	public void inputNumber(){
		//temp variable to store num
		int num;
        Scanner sc  =   new Scanner(System.in);
        System.out.print("Enter first number: ");
        num =   sc.nextInt();
        System.out.print("Enter second number: ");
        num =   sc.nextInt();

	public void printNumber(){
		System.out.println("First Number is --> "+getNum1());
		System.out.println("Second Number is --> "+getNum2());
	public static void main(String[] ar){
		Swap ob = new Swap();


Enter first number: 10
Enter second number: 20
First Number is --> 20
Second Number is --> 10

Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com (2015-2018), Some rights reserved.