;
Java Basics July 16 ,2025

Java Operators – A Complete Guide with Examples and Explanations

Operators are symbols that instruct the compiler to perform specific mathematical, relational, or logical operations. They are fundamental to any programming language and are used extensively to manipulate data and control program flow.

In Java, operators are categorized based on the type of operation they perform. These include:

  1. Arithmetic Operators
  2. Relational (Comparison) Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operators
  6. Ternary (Conditional) Operator

Let’s go through each of these in detail, with both theory and well-explained examples.

1. Arithmetic Operators

Arithmetic operators perform basic mathematical operations. These are used when you want to add, subtract, multiply, divide, or find the remainder of numeric values.

OperatorOperationDescription
+AdditionAdds two operands
-SubtractionSubtracts second operand from the first
*MultiplicationMultiplies both operands
/DivisionDivides numerator by denominator
%ModulusReturns remainder of division

Example:

int a = 15, b = 4;
System.out.println("Addition: " + (a + b));      // 19
System.out.println("Subtraction: " + (a - b));   // 11
System.out.println("Multiplication: " + (a * b));// 60
System.out.println("Division: " + (a / b));      // 3
System.out.println("Modulus: " + (a % b));       // 3

Explanation:

  • a + b adds 15 and 4.
  • a / b performs integer division; fractional part is discarded.
  • a % b returns the remainder when 15 is divided by 4.

2. Relational Operators

Relational operators are used to compare two values or expressions. These comparisons result in a boolean value: true or false.

OperatorMeaningExampleResult
==Equal to5 == 5true
!=Not equal to5 != 3true
>Greater than10 > 8true
<Less than5 < 3false
>=Greater than or equal5 >= 5true
<=Less than or equal4 <= 3false

Example:

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

Explanation:

  • x == y checks equality (false because 10 ≠ 20).
  • x != y checks inequality (true because 10 ≠ 20).
  • These are especially useful in conditions and loops.

3. Logical Operators

Logical operators combine multiple boolean expressions and return a boolean result. They’re commonly used in decision-making constructs.

OperatorNameDescription
&&Logical ANDReturns true if both conditions are true
||Logical ORReturns true if at least one of its operands evaluates to true.
!Logical NOTReverses the boolean value

Example:

int age = 25;
System.out.println(age > 18 && age < 60); // true
System.out.println(age < 18 || age > 65); // false
System.out.println(!(age > 18));          // false

Explanation:

  • &&: true because both conditions are true.
  • ||: false because both conditions are false.
  • !: negates the result of the condition.

4. Bitwise Operators

Bitwise operators perform operations on the individual bits of integer types. They're useful in low-level programming like device drivers and performance-sensitive applications.

 

OperatorNameDescription
&ANDPerforms bitwise AND
|  ORPerforms a logical OR operation on each corresponding bit of two operands.
^XORPerforms bitwise exclusive OR
~NOTPerforms bitwise NOT (inverts bits)
<<Left ShiftShifts bits to the left (multiply by 2)
>>Right ShiftShifts bits to the right (divide by 2)
Complete Reference for Bitwise Operators in Programming/Coding -  GeeksforGeeks

Example:

int a = 5, b = 3;
System.out.println("a & b: " + (a & b));  // 1
System.out.println("a | b: " + (a | b));  // 7
System.out.println("a ^ b: " + (a ^ b));  // 6
System.out.println("~a: " + (~a));        // -6
System.out.println("a << 1: " + (a << 1));// 10
System.out.println("a >> 1: " + (a >> 1));// 2

Explanation:

  • 5 in binary: 0101, 3: 0011
  • &: 0101 & 0011 = 0001 (1)
  • |: 0101 | 0011 = 0111 (7)
  • ^: 0101 ^ 0011 = 0110 (6)
  • ~a: Inverts bits and returns -6 (in 2’s complement)

5. Assignment Operators

Assignment operators assign values to variables. Apart from the standard =, Java supports combined operators that perform an operation and assignment together.

OperatorDescriptionEquivalent Expression
=Assigna = 10
+=Add and assigna = a + 5
-=Subtract and assigna = a - 5
*=Multiply and assigna = a * 2
/=Divide and assigna = a / 2
%=Modulus and assigna = a % 2

Example:

int a = 10;
a += 5;  // a = 15
a *= 2;  // a = 30
a -= 10; // a = 20
System.out.println("Final value: " + a); // 20

Explanation:
These make code shorter and cleaner by reducing redundancy in arithmetic operations followed by assignments.

6. Ternary Operator (Conditional Operator)

The ternary operator is a shorthand for if-else statements and is often used to assign values based on a condition.

Syntax:

variable = (condition) ? valueIfTrue : valueIfFalse;

Example:

int age = 18;
String result = (age >= 18) ? "Eligible" : "Not Eligible";
System.out.println(result);

Explanation:

  • If the condition age >= 18 is true, "Eligible" is assigned to result, otherwise "Not Eligible" is assigned.

Summary Table

CategoryOperatorsPurpose
Arithmetic+, -, *, /, %Basic math operations
Relational==, !=, >, <, >=, <=Comparison, returns boolean values
Logical&&,||, !To combine multiple boolean expressions or to negate a boolean condition.
Bitwise&, | , ^, ~, <<, >>Perform operations at the binary level, manipulating individual bits of integer values.
Assignment=, +=, -=, *=, /=, %=Assign and modify values
Ternarycondition ? expr1 : expr2Short form of if-else

Final Thoughts

Operators allow you to manipulate data and control your program logic. Understanding them well helps you write powerful and expressive Java code. Practice combining different types of operators to build dynamic expressions.

 

Next Blog- Java Control Flow Statements

 

Sanjiv
0

You must logged in to post comments.

Get In Touch

123 Street, New York, USA

+012 345 67890

techiefreak87@gmail.com

© Design & Developed by HW Infotech