Swap two numbers without using third variable in java

Swap two numbers without using third variable in java 

 
Step 1
 
Let's open the notepad and write the following code:
 
Let's try to find the error in this program. if we write the small 's' on the place of capital 'S'.
  1. class demo {  
  2.  public static void main(string arg[]) {  
  3.   System.out.println("Before swapping");  
  4.   int x = 10;  
  5.   int y = 20;  
  6.   System.out.println("value of x:" + x);  
  7.   System.out.println("value of y:" + y);  
  8.   system.out.println("After swapping");  
  9.   x = x + y;  
  10.   y = x - y;  
  11.   x = x - y;  
  12.   System.out.println("value of x:" + x);  
  13.   System.out.println("value of y:" + y);  
  14.  }  
  15. }   
Step 2
 
Name it as "swap.java" and save the file in any location I saved at "c:/app".
  
Step 3
 
Open command prompt(press window + R and write cmd and hit ok).
 
a
 
Step 4
 
Go to "c:/app" by using the command prompt.
 
s
 
Step 5
 
Now write the following code for checking my java file is compiling or not.
  1. javac swap.java  

d
 
My java file does not compile successfully. because there are two errors in this program.
 
So, let me write the program in the right way. and see the output.
  1. class demo {  
  2.  public static void main(String arg[]) {  
  3.   System.out.println("Before swapping");  
  4.   int x = 10;  
  5.   int y = 20;  
  6.   System.out.println("value of x:" + x);  
  7.   System.out.println("value of y:" + y);  
  8.   System.out.println("After swapping");  
  9.   x = x + y;  
  10.   y = x - y;  
  11.   x = x - y;  
  12.   System.out.println("value of x:" + x);  
  13.   System.out.println("value of y:" + y);  
  14.  }  
  15. }   
Step 6
 
Now write the following code for checking my java file is compiling or not.
  1. javac swap.java  
 
f
 
My java program compiled successfully.
 
Step 7
 
Write the following code in the command prompt. Press enter and see the output.
  1. java demo    
  2. // demo is a class name which is written in my "swap.java" file.  
Output
 
g
 
Happy coding.