Operators in Java

Short Answer:
Operators in Java are special symbols used to perform operations on variables and values.

Detailed Explanation:
Java provides different types of operators to perform arithmetic, logical, relational, and other operations.


Types of Operators in Java

Operator Type Examples Usage
Arithmetic + - * / % Mathematical calculations
Unary ++ -- ! Single operand operations
Relational == != > < >= <= Comparison between values
Logical && || ! Combine conditions
Assignment = += -= *= /= Assign values
Bitwise & | ^ ~ Bit-level operations
Shift << >> >>> Bit shifting
Ternary condition ? value1 : value2 Short form of if-else
instanceof instanceof Check object type

Example

public class Test {
    public static void main(String[] args) {

        int a = 10, b = 5;

        // Arithmetic
        int sum = a + b;

        // Relational
        boolean result = a > b;

        // Logical
        boolean check = (a > 0 && b > 0);

        // Ternary
        int max = (a > b) ? a : b;

        System.out.println(max);
    }
}

Important Interview Points

  • Operators follow precedence rules.
  • Logical operators use short-circuit evaluation.
  • == compares values for primitives but references for objects.
  • equals() method compares object content.
  • Ternary operator improves code readability.

0 comments

Leave a comment