Type Conversion and Type Casting 3.9
Swapping variables without using a third variable: In the method SwapVars() of Example
3.2, we have used a third variable called temp to swap the variable. Can we achieve the same
result without using the third variable? Refer to the code segment shown below:
1.
public void SwapVars(int x , int y){ // suppose x=10 & y=100
2. x=x+y; // x now becomes 110
3. y=x-y; // y now becomes 110-100 =10
4. x=x-y; // y now becomes 110-10 = 100
5. System.out.print(“X&Y vars inside Swap Method afetr swap-
ping”);
6. System.out.println(“ X = “ +x + “ Y = “ + y);}
7. }
This code is more efficient as we have not used a third variable.
3.6 Arithmetic Operators
The basic (also known as intrinsic) operators are
+ addition