Java Operators

An operator is a symbol that performs an operation on one or more values (called operands). Operators are the building blocks of any expression or calculation in Java. For example, in 5 + 3, the + is the operator and 5 and 3 are the operands.

Java provides a rich set of operators grouped by their purpose.

1. Arithmetic Operators

Arithmetic operators perform basic mathematical operations.

OperatorNameExampleResult
+Addition10 + 313
-Subtraction10 - 37
*Multiplication10 * 330
/Division10 / 33 (integer division)
%Modulus (Remainder)10 % 31
public class ArithmeticDemo {
    public static void main(String[] args) {
        int a = 10, b = 3;
        System.out.println("Addition: " + (a + b));       // 13
        System.out.println("Subtraction: " + (a - b));    // 7
        System.out.println("Multiplication: " + (a * b)); // 30
        System.out.println("Division: " + (a / b));       // 3
        System.out.println("Remainder: " + (a % b));      // 1
    }
}

Note: When dividing two integers in Java, the result is always an integer. Any decimal part is discarded. For decimal results, use double.

2. Assignment Operators

Assignment operators store values into variables. The basic assignment operator is =. There are also shorthand compound assignment operators.

OperatorMeaningEquivalent To
=Assignx = 5
+=Add and assignx = x + 5
-=Subtract and assignx = x - 5
*=Multiply and assignx = x * 5
/=Divide and assignx = x / 5
%=Modulus and assignx = x % 5
int x = 10;
x += 5;   // x is now 15
x -= 3;   // x is now 12
x *= 2;   // x is now 24
System.out.println("x = " + x);   // Output: x = 24

3. Comparison (Relational) Operators

Comparison operators compare two values and return a boolean result — either true or false. These are heavily used in conditions and loops.

OperatorMeaningExampleResult
==Equal to5 == 5true
!=Not equal to5 != 3true
>Greater than8 > 3true
<Less than3 < 8true
>=Greater than or equal5 >= 5true
<=Less than or equal4 <= 6true
int p = 10, q = 20;
System.out.println(p == q);   // false
System.out.println(p != q);   // true
System.out.println(p < q);    // true
System.out.println(p >= 10);  // true

4. Logical Operators

Logical operators combine multiple boolean conditions.

OperatorNameDescriptionExample
&&Logical ANDReturns true if both conditions are truetrue && false → false
||Logical ORReturns true if at least one condition is truetrue || false → true
!Logical NOTReverses the boolean value!true → false
int age = 20;
boolean hasID = true;

// Both conditions must be true
System.out.println(age >= 18 && hasID);   // true

// At least one condition must be true
System.out.println(age < 18 || hasID);    // true

// Reversal
System.out.println(!hasID);               // false

5. Increment and Decrement Operators

These operators increase or decrease a numeric value by 1. They exist in two forms: prefix (before the variable) and postfix (after the variable).

OperatorTypeEffect
++xPrefix incrementIncrements first, then uses the value
x++Postfix incrementUses the value first, then increments
--xPrefix decrementDecrements first, then uses the value
x--Postfix decrementUses the value first, then decrements
int n = 5;
System.out.println(n++);   // prints 5, then n becomes 6
System.out.println(n);     // prints 6
System.out.println(++n);   // n becomes 7, then prints 7

6. Ternary Operator

The ternary operator is a shorthand for an if-else statement. It takes three parts: a condition, a value if true, and a value if false.

variable = (condition) ? valueIfTrue : valueIfFalse;
int marks = 75;
String result = (marks >= 50) ? "Pass" : "Fail";
System.out.println(result);   // Output: Pass

7. Bitwise Operators (Overview)

Bitwise operators work on individual bits of integer values. These are more advanced and used in low-level programming tasks.

OperatorNameExample
&Bitwise AND5 & 3 → 1
|Bitwise OR5 | 3 → 7
^Bitwise XOR5 ^ 3 → 6
~Bitwise NOT~5 → -6
<<Left shift5 << 1 → 10
>>Right shift10 >> 1 → 5

Operator Precedence

When multiple operators appear in the same expression, Java evaluates them in a specific order called operator precedence (similar to BODMAS in mathematics).

General precedence from highest to lowest:

  1. Parentheses ()
  2. Unary operators: ++, --, !
  3. Multiplication, division, modulus: *, /, %
  4. Addition, subtraction: +, -
  5. Comparison: <, >, <=, >=
  6. Equality: ==, !=
  7. Logical AND: &&
  8. Logical OR: ||
  9. Assignment: =, +=, etc.
int result = 10 + 2 * 5;     // 2*5 is evaluated first → 10 + 10 = 20
int result2 = (10 + 2) * 5; // parentheses first → 12 * 5 = 60

Summary

  • Java operators perform operations on values and variables.
  • Arithmetic operators: +, -, *, /, %.
  • Assignment operators: =, +=, -=, etc.
  • Comparison operators return true or false.
  • Logical operators combine boolean conditions using AND, OR, NOT.
  • The ternary operator is a compact form of an if-else.
  • Operator precedence determines evaluation order; use parentheses to control it explicitly.

Leave a Comment

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