Operators in Java with Examples | Java Programming Guide

7/25/2025

Diagram Operators in Java with Example

Go Back

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.

OperatorDescriptionExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulusa % 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.

OperatorDescriptionExample
==Equal toa == b
!=Not equal toa != b
>Greater thana > b
<Less thana < b
>=Greater than or equala >= b
<=Less than or equala <= 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.

OperatorDescriptionExample
&&Logical ANDa > 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.

OperatorDescriptionExample
=Assigna = 10
+=Add and assigna += 5
-=Subtract and assigna -= 5
*=Multiply and assigna *= 5
/=Divide and assigna /= 5
%=Modulus and assigna %= 5

Example:

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

5. Unary Operators

Operate on a single operand.

OperatorDescriptionExample
+Unary plus+a
-Unary minus-a
++Incrementa++
--Decrementa--
!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.

OperatorDescriptionExample
&Bitwise ANDa & b
  Bitwise OR
^Bitwise XORa ^ b
~Bitwise complement~a
<<Left shifta << 2
>>Right shifta >> 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.

OperatorDescriptionSyntax
?:Ternary conditionalcondition ? 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.

 

 Diagram  Operators in Java with Example

Table of content