Hello guys! welcome to lingarajtechhub.com. Today we are going to discuss swap two numbers using a third variable in java.
package com.swap.java; import java.util.Scanner; public class Swapping_using_3rd_variable { public static void main(String[] args) { int a,b,c; Scanner Sc=new Scanner(System.in); System.out.println("enter the value of a,b"); a=Sc.nextInt( ); b=Sc.nextInt( ); System.out.println("before swapping the value of a is" +a ); System.out.println("before swapping the value of b is" +b ); c=a; a=b; b=c; System.out.println("after swapping the value of a is" +a ); System.out.println("after swapping the value of b is" +b ); } }
Line-1:
❖ Package com.swap.java;
It is a java package( com.swap.java) which is a collection of classes and this package is created in a java project.
Line-2:
❖ import java.util.Scanner;
A scanner is a class in java. util package used for obtaining the input of the primitive types like int, double, etc. and strings.
Line-3:
❖ public class Swapping_using_3rd_variable
*Here Swapping_using_3rd_variable is defined as a class which is a template used to create objects and to define object data types and methods. >–and it’s defined as public because it can be accessed anywhere.
Line-4:
❖ public static void main(String[] args)
>>– it is a main method in which we can execute a program //public :it is an access modifier of the main function which must be public so that the JVM which is (Java Virtual Machine)can access and execute this method.
//If a method is not public, then access is restricted and its showing an error //static is a keyword in which the main method must to be static in which JVM can load the class into memory and call the main method without creating an instance //void it means no return type
//main() is function string[]args : string means a collection of characters, String[] args represents a collection of Strings that are separated by a space and can be typed into the program on the terminal directly
Line-6:
❖ int a,b,c;
>>– here we declare a,b,c as a variable which is an integer type
Line-7:
❖ Scanner sc=new Scanner(System.in);
>>–it means here scanner is a class and Sc is the instance of the object which is a new Scanner.System.in is an argument
Line-8:
❖ System.out.println(“Enter the value of a,b “);
>>– it means System is a class name and out is an instance of the System println is print a new line
Line-9:
❖ a=sc.nextInt();
>>– here s is a variable that we take and Sc is our instance name which is created for a object and nextInt() is a method of a Scanner object that reads in a string of digits or char and converts them into an int type
Line-10:
❖ b=sc.nextInt();
>>– here s is a variable that we take and Sc is our instance name, which is created for an object and nextInt() is a method of a Scanner object that reads in a string of digits or char and converts them into an int type.
Line-11:
❖ System.out.println(“before swapping the value of a is” +a);
>>– it means System is a class name and out is an instance of the System println is print a new line.
>>–before swapping the value is an expression.
>>– +a means concatenate or comma operator.
Line-12:
❖ System.out.println(“before swapping the value of b is” +b);
>>– it means System is a class name and out is an instance of the System println is print a new line.
>>–before swapping the value is b, an expression.
>>– +b means coordinate or comma operator.
Line-13:
❖c=a;
a=b;
b=c;
>>–c=a; means c is empty so the value of a is transferred into c, then a=b; means the value of b transfers into a, then b=c; means the value of c transfers into b.
Line-16:
❖ System.out.println(“after swapping the value of a is” +a);
>>– it means System is a class name and out is an instance of the System println is print a new line.
>>–after swapping the value is an expression.
>>– +a means concatenate or comma operator.
Line-17:
❖ System.out.println(“After swapping the value of b is” +b);
>>– it means System is a class name and out is an instance of the System println is print a new line.
>>–after swapping the value is b , an expression.
>>– +b means concardinate or comma operator.