 Hello friends. I am Sanjay Gupta. I welcome you on my channel. If you like this video, you can like and share these programming related videos with your friends. In this video, I am going to discuss a logic with you that is swap values of two variables using and without using third variable in Java. So there are two concepts which will be demonstrated in this video by me. First one is a swapping using third variable and second one will be swapping without using third variable. So first of all, I am going to import scanner class. Scanner class is required to receive input from user through output screen or you can say console. So that's why I have imported this class. Then this is user defined class whose name is swap. Inside this class, I am going to define main function. Inside this function, I am going to write definition so that we can see how we can swap values of two variables. Here I have declared three variables and one and two and ten. So first of all, I am going to display a message on output screen with the help of system.out.println. This statement will print enter first number message on output screen to receive that number. I need instance of scanner class. So first I am going to create that instance and in constructor of this scanner class instance, I am passing system.in. It means all the input will be received from console. Now here I am using input.nexting. So this way first number will be received from user and that number will be stored inside N1. Now to read second number, I am going to reuse these two statements which are available at line number 9 and 10. I have pasted those two statements and I am modifying them to see second number. So I hope you have understood till now. Till now I have written the code to read two numbers from user. Now I am going to write the code which will swap two numbers using third variable. So first I am going to store value of N1 into 10. Now I am going to assign value of N2 into N1 and then assigning value of 10 into N2. So this way I have interchanged values of N1 and N2. Now we have to display these values on output screen so that we can compare whether they are swapped or not. So number 1 equals 2 plus N1. So N1 variable will display number 1 and 2 will display number 2. So we have to see whether N1 is printing its original value that is provided by user or it will print value of N2 that is available in it after swapping. So this is the implementation of logic that is swapping two numbers using third variable. I have saved this code with a swap dot Java name and it is saved in D drive inside Java folder. So now I am going to open command prompt moving into D drive then Java folder. Now I have to compile this code. So I am writing Java C swap dot Java. So you can see it is compiled properly. No error is available. It means our code is appropriate and it is written correctly. Now I can run this code with Java space name of class that is swap. It is asking two numbers. I am entering 10. Second number is 20. So output should be 20 and 10. You can see number one is 20 and number two is 10. So number one is printed with the help of N1. So initially N1 was 10 that is provided by user. Now it is printing 20. It means values of N1 and N2 are interchanged properly. Now I am minimizing this command prompt. So after implementation of swapping using third variable. Now I have to convert this logic into swapping without using third variable. So I am removing this stamp variable because it is not required. Now first statement is N1 equals to N1 plus N2. Then N2 equals to N1 minus N2 and N1 equals to N1 minus N2. So these three statements are required to swap two numbers without using third variable. So first I have added N1 and N2 and the result will be assigned into N1. Then difference of N1 and 2 will be assigned into N2. Then again difference of N1 and 2 will be assigned into N1. So let's see whether this logic works or not. So I have modified the source code. So this time I am going to compile it again. It is compiled properly. I am executing this. Sorry I am executing this code again. It is asking for first number. I am entering 30 second number 40. You can see the output still it is working properly. Number one is 40 and number two is 30. So this way I hope you have understood how we can swap values of two variables using and without using third variable in Java. I hope you have understood whatever I have explained to you. If you want to watch more programming related videos you can subscribe my YouTube channel. Thank you for watching this video.