operators used in java code example
Example 1: Operators in java
public class LogicalOperatorDemo
{
public static void main(String[] args)
{
System.out.println("Logical AND Operator: ");
int num1 = 20;
int num2 = 30;
int num3 = 40;
boolean output1 = num2 > num1 && num2 < num3;
System.out.println(output1);
boolean output2 = num2 < num1 && num2 < num3;
System.out.println(output1);
boolean output3 = num2 > num1 && num2 > num3;
System.out.println(output3);
boolean output4 = num2 < num1 && num2 > num3;
System.out.println(output4);
System.out.println("Logical OR Operator: ");
output1 = num2 > num1 || num2 < num3;
System.out.println(output1);
output2 = num2 < num1 || num2 < num3;
System.out.println(output2);
output3 = num2 > num1 || num2 > num3;
System.out.println(output3);
output4 = num2 < num1 || num2 > num3;
System.out.println(output4);
System.out.println("Logical NOT Operator: ");
boolean temp = false ;
boolean temp1 = !temp;
System.out.println("Temp: " + temp);
System.out.println("Logical not: " + temp1);
int n1 = 30;
int n2 = 45;
int n3 = 50;
boolean result1 = n1 < n2;
boolean result2 = !(n1 < n2);
System.out.println(result1);
System.out.println(result2);
boolean result3 = n1 < n2 && n2 < n3;
boolean result4 = !(n1 < n2 && n2 < n3);
System.out.println(result3);
System.out.println(result4);
}
}
Example 2: >> and >>> operators in java
>> is a right shift operator shifts all of the bits in a value to the
right to a specified number of times.
int a =15;
a= a >> 3;
The above line of code moves 15 three characters right.
>>> is an unsigned shift operator used to shift right. The places which
were vacated by shift are filled with zeroes