public class OperatorDemo {
public static void main(String[] args) {
// Variable Declaration
int a = 10;
int b = 5;
boolean result;

// Arithmetic Operators
int sum = a + b;
int difference = a – b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;

System.out.println(“Arithmetic Operators:”);
System.out.println(“a + b = ” + sum);
System.out.println(“a – b = ” + difference);
System.out.println(“a * b = ” + product);
System.out.println(“a / b = ” + quotient);
System.out.println(“a % b = ” + remainder);

// Relational Operators
result = a > b;
System.out.println(“\nRelational Operators:”);
System.out.println(“a > b = ” + result);
result = a < b;
System.out.println(“a < b = ” + result);
result = a == b;
System.out.println(“a == b = ” + result);
result = a != b;
System.out.println(“a != b = ” + result);
result = a >= b;
System.out.println(“a >= b = ” + result);
result = a <= b;
System.out.println(“a <= b = ” + result);

// Logical Operators
boolean x = true;
boolean y = false;

System.out.println(“\nLogical Operators:”);
System.out.println(“x && y = ” + (x && y));
System.out.println(“x || y = ” + (x || y));
System.out.println(“!x = ” + (!x));

// Bitwise Operators
int bitwiseAnd = a & b;
int bitwiseOr = a | b;
int bitwiseXor = a ^ b;
int bitwiseComplement = ~a;
int leftShift = a << 1;
int rightShift = a >> 1;

System.out.println(“\nBitwise Operators:”);
System.out.println(“a & b = ” + bitwiseAnd);
System.out.println(“a | b = ” + bitwiseOr);
System.out.println(“a ^ b = ” + bitwiseXor);
System.out.println(“~a = ” + bitwiseComplement);
System.out.println(“a << 1 = ” + leftShift);
System.out.println(“a >> 1 = ” + rightShift);

// Assignment Operators
int c = 15;
c += 5; // Equivalent to c = c + 5
System.out.println(“\nAssignment Operators:”);
System.out.println(“c after c += 5 is ” + c);

c -= 3; // Equivalent to c = c – 3
System.out.println(“c after c -= 3 is ” + c);

c *= 2; // Equivalent to c = c * 2
System.out.println(“c after c *= 2 is ” + c);

c /= 4; // Equivalent to c = c / 4
System.out.println(“c after c /= 4 is ” + c);

c %= 2; // Equivalent to c = c % 2
System.out.println(“c after c %= 2 is ” + c);
}
}


Leave a Reply

Your email address will not be published. Required fields are marked *