JAVA Syllabus
QUESTIONS & ANSWERS

Explain different types of Operators in java?

In Java, operators are used to perform operations on operands (variables or values). There are several types of operators in Java, including:

  1. Arithmetic Operators:
    These are used to perform basic arithmetic operations such as addition, subtraction, multiplication, division, and modulus (remainder). They are represented by symbols such as "+", "-", "*", "/", and "%". For example:
int a = 10, b = 5;
int c = a + b;    // c is 15
int d = a * b;    // d is 50
int e = a % b;    // e is 0 (since 10 is perfectly divisible by 5)
  1. Assignment Operators:
    These are used to assign values to variables. They are represented by symbols such as "=", "+=", "-=", "*=", "/=", and "%=". For example:
int a = 10;
a += 5;    // equivalent to a = a + 5; (a is now 15)
a *= 2;    // equivalent to a = a * 2; (a is now 30)
  1. Comparison Operators:
    These are used to compare two values and return a boolean result (true or false). They are represented by symbols such as "==", "!=", "<", ">", "<=", and ">=". For example:
int a = 10, b = 5;
boolean result = (a > b);    // result is true
result = (a == b);    // result is false
  1. Logical Operators:
    These are used to combine multiple boolean values and return a boolean result. They are represented by symbols such as "&&" (logical AND), "||" (logical OR), and "!" (logical NOT). For example:
boolean a = true, b = false, c = true;
boolean result = (a && b);    // result is false
result = (a || b);    // result is true
result = !c;    // result is false
  1. Bitwise Operators:
    These are used to perform operations on binary values (values in 0 and 1 form). They are represented by symbols such as "&" (bitwise AND), "|" (bitwise OR), "^" (bitwise XOR), "~" (bitwise NOT), "<<", and ">>" (bitwise shift left and right). For example:
int a = 0b1010, b = 0b1100;    // 0b prefix is used to indicate binary numbers
int result = (a & b);    // result is 0b1000 (binary AND)
result = (a | b);    // result is 0b1110 (binary OR)
result = (a ^ b);    // result is 0b0110 (binary XOR)
result = ~a;    // result is 0b11111111111111111111111111110101 (binary NOT)

These are the most commonly used operators in Java. Understanding them is essential for writing effective Java programs.

03/03/2023, 11:21 am Read : 124 times