0
Explore
0

Operators in Java

Updated on July 27, 2025

Welcome to your next step in learning Java! In this tutorial, we’ll take a deep dive into two fundamental concepts:

  • Operators (used to perform operations on variables and values)
  • Type Casting (used to convert data types)

These are basic yet powerful tools that every Java programmer must understand.

What is an Operator?

An operator is a symbol that performs a specific operation on one, two, or more operands (variables or values). In Java, operators are used for performing arithmetic, comparison, logical, assignment, and other operations.

1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, etc.

These operators perform basic mathematical operations:

OperatorMeaningExampleResult
+Addition10 + 515
-Subtraction10 - 55
*Multiplication10 * 550
/Division10 / 25
%Modulus (remainder)10 % 31

Example:

int a = 10, b = 7;
System.out.println(a + b);  // Output: 17
System.out.println(a - b);  // Output: 3
System.out.println(a * b);  // Output: 70
System.out.println(a / b);  // Output: 1
System.out.println(a % b);  // Output: 3

2. Unary Operators

Unary operators work on a single operand to perform various operations like increment, decrement, negation, etc.

Operators that works on a single operand.

OperatorMeaningExample
+Unary plus+a
-Unary minus-a
++Increment (adds 1)a++ or ++a
--Decrement (subtracts 1)a-- or --a

Pre vs Post Increment:

  • ++a: Increments first, then uses the value.
  • a++: Uses the value, then increments.

Example:

int a = 5;
System.out.println(+a);   // Output: 5
System.out.println(-a);   // Output: -5
System.out.println(++a);  // Output: 6 (pre-increment)
System.out.println(a--);  // Output: 6 (post-decrement, a becomes 5)
boolean flag = false;
System.out.println(!flag); // Output: true

3. Assignment Operators

Assignment operators are used to assign values to variables.

OperatorMeaningExample
=Assigna = 5
+=Add and assigna += 3 (same as a = a + 3)
-=Subtract and assigna -= 2
*=Multiply and assigna *= 2
/=Divide and assigna /= 2
%=Modulo and assigna %= 3

Example:

int a = 10;
a += 5;  // a = 15
a -= 3;  // a = 12
a *= 2;  // a = 24
a /= 4;  // a = 6
a %= 5;  // a = 1

4. Relational (Comparison) Operators

Relational operators are used to compare two values and return a boolean result: true or false.

OperatorMeaningExampleResult
==Equal toa == btrue/false
!=Not equal toa != btrue/false
>Greater thana > btrue/false
<Less thana < btrue/false
>=Greater than or equal toa >= btrue/false
<=Less than or equal toa <= btrue/false

Example

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

5. Logical Operators

Logical operators are used to combine multiple boolean expressions and return a boolean result (true or false).

OperatorMeaningExample
&&Logical ANDa > 5 && b < 10
``
!Logical NOT!(a > 5)

Example:

int a = 8, b = 4;
System.out.println(a > 5 && b < 10);  // true (both true)
System.out.println(a < 5 || b < 3);   // false (both false)
System.out.println(!(a == b));        // true (a != b)

6. Bitwise Operators (Advanced)

Bitwise operators perform operations bit-by-bit on integer values. They are mostly used in low-level programming, such as device drivers, embedded systems, and performance-critical tasks.

OperatorNameDescriptionExample
&Bitwise AND1 if both bits are 1a & b
``Bitwise OR1 if at least one bit is 1
^Bitwise XOR1 if bits are differenta ^ b
~Bitwise ComplementInverts each bit~a
<<Left ShiftShifts bits left (multiplies by 2ⁿ)a << 2
>>Right ShiftShifts bits right (divides by 2ⁿ)a >> 2
>>>Unsigned Right ShiftFills 0s from the lefta >>> 2

Example

int a = 5;    // 0101 in binary
int b = 3;    // 0011 in binary

System.out.println(a & b);   // 1  (0001)
System.out.println(a | b);   // 7  (0111)
System.out.println(a ^ b);   // 6  (0110)
System.out.println(~a);      // -6 (inverts all bits)
System.out.println(a << 1);  // 10 (0101 becomes 1010)
System.out.println(a >> 1);  // 2  (0101 becomes 0010)

7. Ternary Operator

The ternary operator is a shorthand for if-else. It evaluates a condition and returns one of two values based on whether the condition is true or false.

Syntax

condition ? value_if_true : value_if_false;
PartMeaning
conditionBoolean expression to evaluate
?Separates condition from true result
:Separates true result from false result

Example

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

Test Your Knowledge

  1. What will be the result of 10 % 3?
  2. Is a += 5 same as a = a + 5?
  3. What is the difference between a++ and ++a?
  4. What does the ternary operator do?
  5. Will Java automatically convert a float to int?
  6. Which operator is used to check equality?
  7. What is the output of: javaCopyEditint x = 5; System.out.println(x++ + ++x);