DevAcademia
C++C#CPythonJava
  • Java Basics

  • Java Introduction
  • Java Get Started
  • Java Syntax
  • Java Output
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Type Casting
  • Java Operators
  • Java Strings
  • Java If...Else
  • Java Switch Statement
  • Java Loops
  • Java Math
  • Java Arrays
  • Java Date
  • Java OOP

  • Java Classes/Objects
  • Java Class Attributes
  • Java Class Methods
  • Java Constructors
  • Java Destructors
  • Java this Keyword
  • Java Modifiers
  • Java Non Modifiers
  • Java Encapsulation
  • Java Packages & API
  • Java Inheritance
  • Java Polymorphism
  • Java Super Keyword
  • Java Inner Classes
  • Java Exception Handling
  • Java Abstraction
  • Java Interfaces
  • Java Enums
  • Java User Input
  • Java Quiz

  • Java Fundamentals Quiz
  • Java Basics

  • Java Introduction
  • Java Get Started
  • Java Syntax
  • Java Output
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Type Casting
  • Java Operators
  • Java Strings
  • Java If...Else
  • Java Switch Statement
  • Java Loops
  • Java Math
  • Java Arrays
  • Java Date
  • Java OOP

  • Java Classes/Objects
  • Java Class Attributes
  • Java Class Methods
  • Java Constructors
  • Java Destructors
  • Java this Keyword
  • Java Modifiers
  • Java Non Modifiers
  • Java Encapsulation
  • Java Packages & API
  • Java Inheritance
  • Java Polymorphism
  • Java Super Keyword
  • Java Inner Classes
  • Java Exception Handling
  • Java Abstraction
  • Java Interfaces
  • Java Enums
  • Java User Input
  • Java Quiz

  • Java Fundamentals Quiz

Loading Java tutorial…

Loading content
Java BasicsTopic 20 of 59
←PreviousPrevNextNext→

Java Operators - Complete Guide

Introduction to Java Operators

Operators in Java are special symbols that perform operations on operands and return a result.

They are the foundation of expressions and are grouped into categories: arithmetic, assignment, comparison, logical, bitwise, ternary, and more.

Arithmetic Operators

Arithmetic operators handle basic math operations like addition, subtraction, multiplication, division, and modulus. Java also supports increment and decrement operators.

Example
public class ArithmeticOperators {
    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));

        int c = 5;
        System.out.println("c++ = " + (c++)); // Post-increment
        System.out.println("++c = " + (++c)); // Pre-increment

        int d = 8;
        System.out.println("d-- = " + (d--)); // Post-decrement
        System.out.println("--d = " + (--d)); // Pre-decrement
    }
}
Output
a + b = 13
a - b = 7
a * b = 30
a / b = 3
a % b = 1
c++ = 5
++c = 7
d-- = 8
--d = 6

Assignment and Comparison Operators

Assignment operators assign values, often in shorthand form. Comparison operators evaluate relationships between values, returning boolean results.

Example
public class AssignmentComparison {
    public static void main(String[] args) {
        int x = 10;
        x += 5;
        x -= 3;
        x *= 2;
        x /= 4;
        x %= 3;
        System.out.println("x after operations: " + x);

        int a = 5, b = 8;
        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));
        System.out.println("a <= b: " + (a <= b));
    }
}
Output
x after operations: 0
a == b: false
a != b: true
a > b: false
a < b: true
a >= b: false
a <= b: true

Logical and Bitwise Operators

Logical operators combine boolean expressions, while bitwise operators manipulate individual bits of integers.

Example
public class LogicalBitwise {
    public static void main(String[] args) {
        boolean p = true, q = false;
        System.out.println("p && q: " + (p && q));
        System.out.println("p || q: " + (p || q));
        System.out.println("!p: " + (!p));

        int x = 5; // 0101
        int y = 3; // 0011
        System.out.println("x & y: " + (x & y));
        System.out.println("x | y: " + (x | y));
        System.out.println("x ^ y: " + (x ^ y));
        System.out.println("~x: " + (~x));
        System.out.println("x << 1: " + (x << 1));
        System.out.println("x >> 1: " + (x >> 1));
        System.out.println("x >>> 1: " + (x >>> 1));
    }
}
Output
p && q: false
p || q: true
!p: false
x & y: 1
x | y: 7
x ^ y: 6
~x: -6
x << 1: 10
x >> 1: 2
x >>> 1: 2

Ternary and instanceof Operators

The ternary operator `?:` is a compact if-else. The `instanceof` operator checks if an object belongs to a particular class.

Example
public class TernaryInstanceof {
    public static void main(String[] args) {
        int age = 20;
        String status = (age >= 18) ? "Adult" : "Minor";
        System.out.println("Status: " + status);

        int score = 85;
        String grade = (score >= 90) ? "A" : (score >= 80) ? "B" : "C";
        System.out.println("Grade: " + grade);

        Object obj = "Hello";
        System.out.println("obj instanceof String: " + (obj instanceof String));
        System.out.println("obj instanceof Object: " + (obj instanceof Object));
    }
}
Output
Status: Adult
Grade: B
obj instanceof String: true
obj instanceof Object: true

Operator Precedence and Best Practices

Operator precedence controls evaluation order. Using parentheses improves readability and prevents logical errors.

  • ✅ Always use parentheses for clarity in complex expressions
  • ✅ Remember that integer division truncates results
  • ✅ Prefer `&&` and `||` for short-circuiting instead of `&` and `|`
  • ✅ Keep ternary usage simple; avoid nested ternaries for readability
  • ✅ Use compound assignments like `x += 5` to simplify expressions
PrecedenceOperatorDescriptionAssociativity
Highest() [] .Parentheses, array access, member accessLeft to right
++ -- ! ~Unary operatorsRight to left
* / %MultiplicativeLeft to right
+ -AdditiveLeft to right
<< >> >>>ShiftLeft to right
< <= > >= instanceofRelationalLeft to right
== !=EqualityLeft to right
&Bitwise ANDLeft to right
^Bitwise XORLeft to right
|Bitwise ORLeft to right
&&Logical ANDLeft to right
||Logical ORLeft to right
?:TernaryRight to left
Lowest= += -= *= /= %=AssignmentRight to left
Test your knowledge: Java Operators - Complete Guide
Quiz Configuration
4 of 10 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Java BasicsTopic 20 of 59
←PreviousPrevNextNext→