Java Operators

 

Operators in Java are special symbols that perform operations on variables and values. They form the building blocks of expressions and logic in Java programs. For example, the + operator adds two numbers, while the == operator compares two values.

Understanding Java operators is essential because they are used everywhere in programming — from arithmetic calculations to decision-making and logic building.

Types of Java Operators

 

Java provides a rich set of operators, which can be grouped into several categories:

  1. Arithmetic Operators
  2. Relational (Comparison) Operators
  3. Logical Operators
  4. Assignment Operators
  5. Unary Operators
  6. Bitwise Operators
  7. Ternary (Conditional) Operator
  8. Type Comparison Operator (instanceof)

Let’s explore each in detail.

1. Arithmetic Operators

 

These operators are used to perform basic mathematical operations.

OperatorDescriptionExample (a=10, b=5)Result
+Additiona + b15
-Subtractiona - b5
*Multiplicationa * b50
/Divisiona / b2
%Modulus (Remainder)a % b0

Example:

 

public class ArithmeticExample {
  public static void main(String[] args) {
    int a = 10, b = 3;
    System.out.println(“a + b = ” + (a + b));
    System.out.println(“a – b = ” + (a – b));
    System.out.println(“a * b = ” + (a * b));
    System.out.println(“a / b = ” + (a / b));
    System.out.println(“a % b = ” + (a % b));
  }
}

2. Relational (Comparison) Operators

 

These operators compare two values and return a boolean result (true or false).

OperatorDescriptionExample (a=10, b=20)Result
==Equal toa == bfalse
!=Not equal toa != btrue
>Greater thana > bfalse
<Less thana < btrue
>=Greater than or equal toa >= bfalse
<=Less than or equal toa <= btrue

Example:

 

public class RelationalExample {
  public static void main(String[] args) {
    int a = 10, b = 20;
    System.out.println(“a == b : ” + (a == b));
    System.out.println(“a != b : ” + (a != b));
    System.out.println(“a > b : ” + (a > b));
    System.out.println(“a < b : ” + (a < b));
  }
}

3. Logical Operators

 

These operators are used to combine multiple conditions.

OperatorDescriptionExample (a=10, b=20)Result
&&Logical AND(a < 15 && b > 15)true
` `Logical OR
!Logical NOT!(a < b)false

Example:

 

public class LogicalExample {
  public static void main(String[] args) {
    int a = 10, b = 20;
    System.out.println((a < 15 && b > 15)); // true
    System.out.println((a > 15 || b > 15)); // true
    System.out.println(!(a < b)); // false
  }
}

4. Assignment Operators

 

These operators assign values to variables.

OperatorExampleEquivalent To
=a = 5a = 5
+=a += 5a = a + 5
-=a -= 5a = a - 5
*=a *= 5a = a * 5
/=a /= 5a = a / 5
%=a %= 5a = a % 5

Example:

 

public class AssignmentExample {
  public static void main(String[] args) {
    int a = 10;
    a += 5; // a = a + 5
    System.out.println(“a = ” + a);
  }
}

5. Unary Operators

 

These operators work with a single operand.

OperatorDescriptionExample (a=5)Result
+Unary plus+a5
-Unary minus-a-5
++Incrementa++ or ++a6
--Decrementa-- or --a4
!Logical NOT!(a < 10)false

Example:

 

public class UnaryExample {
  public static void main(String[] args) {
    int a = 5;
    System.out.println(“a++ = ” + (a++)); // Post-increment
    System.out.println(“++a = ” + (++a)); // Pre-increment
  }
}

6. Bitwise Operators

 

These operators perform operations at the bit level.

OperatorDescriptionExample (a=5, b=3)Binary OperationResult
&Bitwise ANDa & b0101 & 0011 = 00011
``Bitwise OR`ab`
^Bitwise XORa ^ b0101 ^ 0011 = 01106
~Bitwise NOT~a~0101 = 1010 (2’s comp)-6
<<Left Shifta << 10101 << 1 = 101010
>>Right Shifta >> 10101 >> 1 = 00102
>>>Unsigned Shifta >>> 1Similar to >> but fills with 02

Example:

 

public class BitwiseExample {
  public static void main(String[] args) {
    int a = 5, b = 3;
    System.out.println(“a & b = ” + (a & b));
    System.out.println(“a | b = ” + (a | b));
    System.out.println(“a ^ b = ” + (a ^ b));
    System.out.println(“a << 1 = ” + (a << 1));
  }
}

7. Ternary (Conditional) Operator

 

The ternary operator is a shorthand for if-else conditions.
Syntax:

condition ? expression1 : expression2

Example:

 

public class TernaryExample {
  public static void main(String[] args) {
    int a = 10, b = 20;
    int max = (a > b) ? a : b;
    System.out.println(“Maximum is: ” + max);
  }
}

8. Type Comparison Operator (instanceof)

 

The instanceof operator is used to test whether an object is an instance of a specific class or subclass.

Example:

class Animal {}
class Dog extends Animal {}

public class InstanceofExample {
  public static void main(String[] args) {
    Animal obj = new Dog();
    System.out.println(obj instanceof Dog); // true
    System.out.println(obj instanceof Animal); // true
  }
}

Java Operators

Conclusion

 

Operators in Java provide the ability to perform calculations, comparisons, logic building, and decision-making. By mastering arithmetic, relational, logical, assignment, unary, bitwise, ternary, and type comparison operators, you can write powerful and efficient Java programs.

Scroll to Top