Tuesday, June 8, 2021

Java Logic

Swapping of 2 numbers using XOR: 
This method is fast and doesn’t require the use of the 3rd variable.
// A quick way to swap a and b
 a ^= b;
 b ^= a;
 a ^= b; 

Checking if the number is even or odd without using the % operator: 
This trick is not much better than using a % operator but is sometimes efficient (with large numbers). 
Use & operator:
System.out.println((a & 1) == 0 ? "EVEN" : "ODD" );

What is meaning of the += operator  in Java?
The addition assignment operator ( += ) adds the value of the right operand to a variable and assigns the result to the variable.

No comments:

Post a Comment