CodeToConcept
About

java

java Tutorials

Introduction
Java Fundamentals
Java Variables
Java Data Types
Java Type Casting
Java Operators
Java Input/Output
Control Statements
Java Class & Object
java Method
Java Constructor
Java this keyword
OOP Principles
Java super keyword
Coming Soon Topics

java Tutorials

Introduction
Java Fundamentals
Java Variables
Java Data Types
Java Type Casting
Java Operators
Java Input/Output
Control Statements
Java Class & Object
java Method
Java Constructor
Java this keyword
OOP Principles
Java super keyword
Coming Soon Topics

Sample Question

Think it through, then check the tutorial.

CodeToConcept

Quickly learn through simple, structured, and practical coding tutorials. Build your confidence from step zero to real-world experience.

© 2026 CodeToConcept. All rights reserved.

Quick Links

AboutTutorials

Tutorials

Java

Support

Privacy PolicyTerms of ServiceContact
GitHubTwitterYouTube

Java Operators

Java operators are special symbols that perform operations on one or more variables or values. where variables or values are called operands. They are used to perform different operations.

Types of Java Operators:
  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators (Relational)
  4. Logical Operators
  5. Increment and Decrement Operators (Unary Operators)
  6. Ternary Operator
  7. Bitwise and Shift Operators
  8. Type Comparison Operator (Instanceof Operator)

1. Arithmetic Operators:

Arithmetic operators are used to perform basic mathematical calculations such as addition, subtraction, multiplication, division, and modulus on numeric values.

Example:

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int firstNumber = 10;
        int secondNumber = 3;
 
        // Addition
        int sum = firstNumber + secondNumber;
        System.out.println("Addition: " + sum); // Output: 13
 
        // Subtraction
        int difference = firstNumber - secondNumber;
        System.out.println("Subtraction: " + difference); // Output: 7
 
        // Multiplication
        int product = firstNumber * secondNumber;
        System.out.println("Multiplication: " + product); // Output: 30
 
        // Division
        int quotient = firstNumber / secondNumber;
        System.out.println("Division: " + quotient); // Output: 3
 
        // Modulus (Remainder)
        int remainder = firstNumber % secondNumber;
        System.out.println("Modulus: " + remainder); // Output: 1
    }
}

2. Assignment Operators:

The assignment operator (=) is used to assign a value to a variable. It is also used to perform compound assignments, will see in examples below.

Example:

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int number;
 
        // = (Assignment operator)
        number = 5;
        // used to assign a value that is 5
        System.out.println("=  : " + number); // Output: 5
 
        /*
        Example of compound assignments
        */
        // += (Addition assignment)
        number = 10;
        number += 3;
        // similar like -> number = number + 3
        System.out.println("+= : " + number); // Output: 13
 
 
        // -= (Subtraction assignment)
        number = 10;
        number -= 3;
        // similar like -> number = number - 3
        System.out.println("-= : " + number); // Output: 7
 
 
        //   *= (Multiplication assignment)
        number = 10;
        number *= 3;
        //   similar like -> number = number * 3
        System.out.println("*= : " + number); // Output: 30
 
 
        //   /= (Division assignment)
        number = 10;
        number /= 3;
        //   similar like -> number = number / 3
        System.out.println("/= : " + number); // Output: 3
 
 
        //   %= (Modulus assignment)
        number = 10;
        number %= 3;
        //   similar like -> number = number % 3
        System.out.println("%= : " + number); // Output: 1
 
 
        //   &= (Bitwise AND assignment)
        number = 10;
        number &= 3;
        //   similar like -> number = number & 3
        System.out.println("&= : " + number); // Output: 2
 
 
        //   |= (Bitwise OR assignment)
        number = 10;
        number |= 3;
        //   similar like -> number = number | 3
        System.out.println("|= : " + number); // Output: 11
 
 
        //   ^= (Bitwise XOR assignment)
        number = 10;
        number ^= 3;
        //   similar like -> number = number ^ 3
        System.out.println("^= : " + number); // Output: 9
 
 
        //   >>= (Right shift assignment)
        number = 10;
        number >>= 1;
        //   similar like -> number = number >> 1
        System.out.println(">>= : " + number); // Output: 5
 
 
        //   <<= (Left shift assignment)
        number = 10;
        number <<= 1;
        //   similar like -> number = number << 1
        System.out.println("<<= : " + number); // Output: 20
    }
}

3. Comparison Operators (Relational):

Comparison operators are used to compare two values or variables and return a boolean result (true or false).

Example:

 
public class HelloWorld {
 
    public static void main(String[] args) {
 
        int a = 10;
        int b = 20;
 
        // == (equal to)
        System.out.println(a == b);  // false because 10 is not equal to 20
 
        // != (not equal to)
        System.out.println(a != b);  // true because 10 is not equal to 20
 
        // > (greater than)
        System.out.println(a > b);    // false because 10 is not greater than 20
 
        // < (less than)
        System.out.println(a < b);    // true because 10 is less than 20
 
        // >= (greater than or equal to)
        System.out.println(a >= b);   // false because 10 is neither greater nor equal to 20
 
        // <= (less than or equal to)
        System.out.println(a <= b);   // true because 10 is less than 20
    }
}

4. Logical Operators

Logical operators are used to combine different conditions (boolean expressions) and always return a boolean value.

Example:

OperatorNameDescription
&&Logical ANDReturns true if both conditions are true
||Logical ORReturns true if at least one condition is true
!Logical NOTReverses the result (true becomes false, false becomes true)

Example:

 
public class HelloWorld {
 
    public static void main(String[] args) {
 
        int age = 18;
        int marks = 75;
        boolean hasIDCard = true;
 
        // AND (&&)
        System.out.println("AND Operator:");
        System.out.println(age >= 18 && marks >= 60);   // true
        System.out.println(age >= 18 && marks >= 80);   // false
 
        System.out.println();
 
        // OR (||)
        System.out.println("OR Operator:");
        System.out.println(marks >= 80 || hasIDCard);   // true
        System.out.println(marks >= 90 || age < 16);    // false
 
        System.out.println();
 
        // NOT (!)
        System.out.println("NOT Operator:");
        System.out.println(!(age >= 18));              // false
        System.out.println(!hasIDCard);                // false
    }
}

5. Increment and Decrement Operators (Unary Operators)

Increment and decrement operators are also called as Unary Operators in Java used to increase or decrease a variable’s value by 1, It works with one operand.

OperatorNameMeaning
+Unary PlusIndicates positive value
-Unary MinusChanges sign of value
++IncrementIncreases value by 1
--DecrementDecreases value by 1
!Logical NOTReverses boolean value

Example:

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int initialValue = 10;        // Initial integer value
        boolean booleanValue = true;  // Boolean value for logical NOT example
 
        // Unary plus: shows positive value (no change in value)
        System.out.println("Unary Plus: " + (+initialValue));
 
        // Unary minus: changes sign of number
        System.out.println("Unary Minus: " + (-initialValue));
 
        System.out.println("\nIncrement Operators:");
 
        // Pre-increment: first increases value, then prints
        System.out.println(++initialValue); // 11
 
        // Post-increment: first prints value, then increases it
        System.out.println(initialValue++);  // 11
 
        // After post-increment, value is updated
        System.out.println(initialValue);    // 12
 
        System.out.println("\nDecrement Operators:");
 
        // Pre-decrement: first decreases value, then prints
        System.out.println(--initialValue);  // 11
 
        // Post-decrement: first prints value, then decreases it
        System.out.println(initialValue--);  // 11
 
        // After post-decrement, value is updated
        System.out.println(initialValue);    // 10
 
        System.out.println("\nLogical NOT Operator:");
 
        // Logical NOT: reverses boolean value
        System.out.println(!booleanValue); // false
    }
}

6. Ternary Operator

The ternary operator is a short form of if-else statement used to make basic decisions in one line.

Syntax:

condition ? firstvalue : secondvalue ;
// If the condition is true, the first value is selected.
// If the condition is false, the second value is selected.

Example:

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int marks = 60;
 
        // Pass if marks >= 40
        
        String result = (marks >= 40) ? "Pass" : "Fail";
        // If condition is true → first value is picked ("Pass")
        // If condition is false → second value is picked ("Fail")
 
 
        System.out.println("Marks: " + marks);
        System.out.println("Result: " + result);
 
        // Output:
        // Marks: 55
        // Result: Pass
    }
}

7. Bitwise and Shift Operators

Bitwise and shift operators are operators that work directly on the binary (bit-level) representation of integer data types like int, long, short, char, and byte. Commonly used for low-level programming, performance optimization, encryption.

OperatorNameMeaning
&AND1 if both bits are 1
|OR1 if at least one bit is 1
^XOR1 if bits are different
~NOTFlips bits (0 ↔ 1)
<<Left ShiftShifts bits left (multiply by 2)
>>Right ShiftShifts bits right (divide by 2)
>>>Unsigned Right ShiftShifts bits right with zero fill

1. Bitwise AND (&)

Returns 1 only when both bits are 1.

Example:

public class HelloWorld {
    public static void main(String[] args) {
        int a = 5; // 0101
        int b = 3; // 0011
 
        System.out.println(a & b);// output : 1
    }
}

Calculation:

  0101 = (5)
& 0011 = (3)
-------
  0001 = (1)
  1. OR (|) Returns 1 if at least one bit is 1. Example:
public class HelloWorld {
    public static void main(String[] args) {
        int a = 5;
        int b = 3;
 
        System.out.println(a | b);// output : 7
    }
}

Calculation:

  0101 = (5)
| 0011 = (3)
-------
  0111 = (7)
  1. XOR (^) Returns 1 when bits are different. Example:
public class HelloWorld {
    public static void main(String[] args) {
        int a = 5;
        int b = 3;
 
        System.out.println(a ^ b); // output : 6
    }
}

Calculation:

  0101 = (5)
^ 0011 = (3)
-------
  0110 = (6)
  1. NOT (~) Reverses all bits.
public class HelloWorld {
    public static void main(String[] args) {
        int a = 5;
 
        System.out.println(~a); //output : -6
    }
}

Calculation:

Rule: ~n = -(n + 1)
 
~5 = -(5+1)
   = -6
  1. Left Shift (<<) Moves bits to the left. Example:
public class HelloWorld {
    public static void main(String[] args) {
        int a = 5;
 
        System.out.println(a << 1); //output : 10
    }
}

Calculation:

5 = 0101
 
0101 << 1
 
1010 = 10

or

Another way of calculation:

n << 1 = n × 2
n << 2 = n × 4
 
Example:
5 << 1 = 10
5 << 2 = 20
  1. Right Shift (>>) Moves bits to the right. Example:
public class HelloWorld {
    public static void main(String[] args) {
        int a = 20;
 
        System.out.println(a >> 2);// output: 5
    }
}

Calculation:

20 = 10100
 
10100 >> 2
 
00101 = 5

or

Another way of calculation:

n >> 1 = n ÷ 2
n >> 2 = n ÷ 4
 
Example:
20 >> 2 = 5
  1. Unsigned Right Shift (>>>) Similar to >>, but always fills left side with 0. Example:
public class HelloWorld {
    public static void main(String[] args) {
        int a = -20;
 
        System.out.println(a >>> 2);//output :1073741819
    }
}

Calculation:

20 = 00010100
 
20 >>> 2
 
00000101 = 5

8. Type Comparison Operator (instanceof Operator)

The instanceof operator in Java is used to check whether an object is an instance of a specific class, subclass, or interface during runtime.

That is it returns true if the object is an instance of the specified type otherwise false.

Example:

    public class HellowWorld {
        public static void main(String[] args) {
            Cat cat = new Cat();
            Car car = new Car();
 
            System.out.println(cat instanceof Dog);      // true
            System.out.println(cat instanceof Animal);   // true
            System.out.println(car instanceof Animal);   // false
            System.out.println(car instanceof Object);   // true
            System.out.println(cat instanceof Object);   // true
        }
    }
 
    class Animal {
      System.out.println("Animal class");
    }
 
    class Cat extends Animal {
      System.out.println("Dog class");
    }
 
    class Car {
      System.out.println("Car class");
    }
 

See you in next page....🙂

← Java Type CastingJava Input/Output →
← Java Type CastingJava Input/Output →