Java Operators – Complete Guide with Types, Examples, and Usage - Textnotes

Java Operators – Complete Guide with Types, Examples, and Usage


Learn all types of Java operators including arithmetic, relational, logical, bitwise, assignment, and ternary operators with detailed examples and use cases.

Operators in Java – Complete Detailed Tutorial

Operators in Java are special symbols that perform operations on variables and values. Understanding operators is essential for performing mathematical calculations, comparisons, and logical operations in Java programs.

1. Types of Operators in Java

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operators
  6. Unary Operators
  7. Ternary Operator
  8. Shift Operators
  9. Instanceof Operator

2. Arithmetic Operators

  1. Used to perform mathematical operations.
  2. Symbols: + - * / %
OperatorDescriptionExample
+Addition5 + 3 = 8
-Subtraction5 - 3 = 2
*Multiplication5 * 3 = 15
/Division10 / 3 = 3
%Modulus (Remainder)10 % 3 = 1

Example:


int a = 10, b = 3;
System.out.println(a + b); // 13
System.out.println(a % b); // 1

3. Relational Operators

  1. Used to compare two values
  2. Return boolean (true or false)
OperatorDescriptionExample
==Equal to5 == 5 → true
!=Not equal to5 != 3 → true
>Greater than5 > 3 → true
<Less than5 < 3 → false
>=Greater than or equal to5 >= 5 → true
<=Less than or equal to3 <= 5 → true

Example:


int x = 10, y = 20;
System.out.println(x < y); // true
System.out.println(x == y); // false

4. Logical Operators

  1. Used to combine multiple boolean expressions
OperatorDescriptionExample
&&Logical AND(x > 5 && y < 30)
||Logical OR`(x > 5
!Logical NOT!(x > y)

Example:


boolean a = true, b = false;
System.out.println(a && b); // false
System.out.println(a || b); // true
System.out.println(!a); // false

5. Bitwise Operators

  1. Operate on binary bits of numbers
OperatorDescriptionExample
&AND5 & 3 → 1
|OR5 | 3 → 7
^XOR5 ^ 3 → 6
~Complement~5 → -6
<<Left shift5 << 1 → 10
>>Right shift10 >> 1 → 5
>>>Zero-fill right shift-5 >>> 1 → 2147483645

Example:


int a = 5, b = 3;
System.out.println(a & b); // 1
System.out.println(a | b); // 7

6. Assignment Operators

  1. Used to assign values to variables
OperatorDescriptionExample
=Assignx = 5
+=Add & assignx += 5 → x = x + 5
-=Subtract & assignx -= 3 → x = x - 3
*=Multiply & assignx *= 2 → x = x * 2
/=Divide & assignx /= 2 → x = x / 2
%=Modulus & assignx %= 3 → x = x % 3

Example:


int x = 10;
x += 5; // x = 15
x %= 4; // x = 3

7. Unary Operators

  1. Operate on a single operand
OperatorDescriptionExample
+Positive+a
-Negative-a
++Incrementa++ / ++a
--Decrementa-- / --a
!Logical NOT!true → false

Example:


int a = 5;
System.out.println(a++); // 5 (post-increment)
System.out.println(++a); // 7 (pre-increment)

8. Ternary Operator

  1. Short form of if-else
  2. Syntax:

variable = (condition) ? value1 : value2;

Example:


int num = 10;
String result = (num % 2 == 0) ? "Even" : "Odd";
System.out.println(result); // Even

9. instanceof Operator

  1. Checks object type
  2. Returns boolean

Example:


String name = "John";
System.out.println(name instanceof String); // true

10. Example Program Using Multiple Operators


public class OperatorsDemo {
public static void main(String[] args) {
int a = 10, b = 20;

// Arithmetic
System.out.println("Sum: " + (a + b));

// Relational
System.out.println("a > b? " + (a > b));

// Logical
System.out.println("a < b && b > 15? " + (a < b && b > 15));

// Ternary
String result = (a % 2 == 0) ? "Even" : "Odd";
System.out.println("a is " + result);
}
}

Output:


Sum: 30
a > b? false
a < b && b > 15? true
a is Even

11. Summary

  1. Operators are special symbols for operations
  2. Categories: Arithmetic, Relational, Logical, Bitwise, Assignment, Unary, Ternary, instanceof
  3. Essential for calculations, comparisons, and decision-making