Operators in Java with Examples | Java Programming Guide

7/25/2025
All Articles

Diagram Operators in Java with Example

 Operators in Java with Examples | Java Programming Guide

Operators in Java with Example | Java Basics Guide

Introduction to Operators in Java

In Java, operators are special symbols or keywords used to perform operations on variables and values. They are fundamental building blocks of any Java program, allowing developers to perform arithmetic, comparisons, logical operations, and more.

Understanding Java operators is crucial for writing efficient and bug-free code. In this article, we will explore various types of operators in Java along with practical examples.


Types of Operators in Java

Java provides a rich set of operators categorized into the following types:

  1. Arithmetic Operators

  2. Relational (Comparison) Operators

  3. Logical Operators

  4. Assignment Operators

  5. Unary Operators

  6. Bitwise Operators

  7. Ternary Operator

Let’s understand each with examples.


1. Arithmetic Operators

These operators perform basic mathematical operations.

Operator Description Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulus a % b

Example:

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

2. Relational Operators

These are used to compare two values.

Operator Description Example
== Equal to a == b
!= Not equal to a != b
> Greater than a > b
< Less than a < b
>= Greater than or equal a >= b
<= Less than or equal a <= b

Example:

int a = 5, b = 10;
System.out.println(a > b);     // false
System.out.println(a <= b);    // true

3. Logical Operators

Used to perform logical operations, usually with boolean values.

Operator Description Example
&& Logical AND a > 5 && b < 10
     
! Logical NOT !(a > 5)

Example:

boolean x = true, y = false;
System.out.println(x && y);  // false
System.out.println(x || y);  // true

4. Assignment Operators

Used to assign values to variables.

Operator Description Example
= Assign a = 10
+= Add and assign a += 5
-= Subtract and assign a -= 5
*= Multiply and assign a *= 5
/= Divide and assign a /= 5
%= Modulus and assign a %= 5

Example:

int a = 10;
a += 5;     // a = a + 5
System.out.println(a);  // 15

5. Unary Operators

Operate on a single operand.

Operator Description Example
+ Unary plus +a
- Unary minus -a
++ Increment a++
-- Decrement a--
! Logical NOT !a

Example:

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

6. Bitwise Operators

Perform operations on individual bits of numbers.

Operator Description Example
& Bitwise AND a & b
    Bitwise OR
^ Bitwise XOR a ^ b
~ Bitwise complement ~a
<< Left shift a << 2
>> Right shift a >> 2

Example:

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

7. Ternary Operator

Short-hand for if-else statement.

Operator Description Syntax
?: Ternary conditional condition ? expr1 : expr2

Example:

int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println("Max: " + max); // 20

Conclusion

Understanding the different Java operators is essential for writing logic in your programs. Whether you are performing arithmetic, logical checks, or bitwise manipulations, Java operators make your code readable and efficient.

Keep practicing with different combinations and you'll gain better command over Java's operator system.


FAQs

Q1: What is the difference between == and equals() in Java?
== checks reference equality, while equals() checks value equality (especially with objects).

Q2: Is += faster than using a = a + b?
They are functionally the same, but += is more concise and preferred for readability.

 

Article