Operators in Java

Table of contents

Operators in Java are essential building blocks of programming. They allow you to perform calculations, comparisons, and manipulate data. In this guide, we’ll cover all types of operators in Java with examples to make it crystal clear.
1. Types of Operators in Java: An Overview
Java offers a wide variety of operators to handle different types of operations. Broadly, they can be categorized as:
Arithmetic Operators: For mathematical calculations.
Assignment Operators: For assigning values to variables.
Relational and Logical Operators: For comparisons and decision-making.
Bitwise Operators: For operations at the binary level.
Increment, Decrement, and Ternary Operators: For shorthand operations.
Let’s explore each type in detail.
2. Arithmetic Operators: Performing Calculations
Arithmetic operators are used to perform basic mathematical operations. These include:
Operator | Description | Example | Result |
+ | Addition | a + b | Sum of a and b |
- | Subtraction | a - b | Difference of a and b |
* | Multiplication | a * b | Product of a and b |
/ | Division | a / b | Quotient of a and b |
% | Modulus (Remainder) | a % b | Remainder when a is divided by b |
Example in Java
public class ArithmeticExample {
public static void main(String[] args) {
int a = 10, b = 3;
System.out.println("Addition: " + (a + b)); // Output: 13
System.out.println("Subtraction: " + (a - b)); // Output: 7
System.out.println("Multiplication: " + (a * b)); // Output: 30
System.out.println("Division: " + (a / b)); // Output: 3
System.out.println("Modulus: " + (a % b)); // Output: 1
}
}
3. Assignment Operators: Simplify Your Code
Assignment operators are used to assign values to variables. The basic assignment operator is =
. Java also provides compound assignment operators:
Operator | Description | Example | Equivalent To |
= | Assign value | a = b | - |
+= | Add and assign | a += b | a = a + b |
-= | Subtract and assign | a -= b | a = a - b |
*= | Multiply and assign | a *= b | a = a * b |
/= | Divide and assign | a /= b | a = a / b |
%= | Modulus and assign | a %= b | a = a % b |
Example in Java
public class AssignmentExample {
public static void main(String[] args) {
int a = 5;
a += 3; // a = a + 3
System.out.println("After += : " + a); // Output: 8
a *= 2; // a = a * 2
System.out.println("After *= : " + a); // Output: 16
a %= 5; // a = a % 5
System.out.println("After %= : " + a); // Output: 1
}
}
4. Relational and Logical Operators in Java
Relational Operators
These are used to compare two values. The result is a boolean (true
or false
).
Operator | Description | Example | Result |
== | Equal to | a == b | true if a equals b |
!= | Not equal to | a != b | true if a is not equal to b |
> | Greater than | a > b | true if a is greater than b |
< | Less than | a < b | true if a is less than b |
>= | Greater than or equal to | a >= b | true if a is greater than or equal to b |
<= | Less than or equal to | a <= b | true if a is less than or equal to b |
Logical Operators
These combine multiple conditions:
Operator | Description | Example | Result |
&& | Logical AND | a > 5 && b < 3 | true if both conditions are true |
` | ` | Logical OR | |
! | Logical NOT | !(a > 5) | true if the condition is false |
Example in Java
public class RelationalLogicalExample {
public static void main(String[] args) {
int a = 10, b = 5;
System.out.println("a > b: " + (a > b)); // Output: true
System.out.println("a < b: " + (a < b)); // Output: false
System.out.println("a > 5 && b < 10: " + (a > 5 && b < 10)); // Output: true
System.out.println("a < 5 || b < 10: " + (a < 5 || b < 10)); // Output: true
}
}
5. Bitwise Operators: Working with Binary
Bitwise operators operate directly on binary digits. These are useful for low-level programming.
Operator | Description | Example | Result |
& | AND | a & b | Bitwise AND |
` | ` | OR | `a |
^ | XOR | a ^ b | Bitwise XOR |
~ | Complement | ~a | Bitwise complement |
<< | Left shift | a << 2 | Shift bits left by 2 |
>> | Right shift | a >> 2 | Shift bits right by 2 |
Example in Java
public class BitwiseExample {
public static void main(String[] args) {
int a = 5, b = 3; // a = 0101, b = 0011
System.out.println("a & b: " + (a & b)); // Output: 1 (0001)
System.out.println("a | b: " + (a | b)); // Output: 7 (0111)
System.out.println("a ^ b: " + (a ^ b)); // Output: 6 (0110)
System.out.println("~a: " + (~a)); // Output: -6
}
}
6. Understanding Increment, Decrement, and Ternary Operators
Increment and Decrement Operators
These operators increase or decrease a value by 1
:
Operator | Description | Example | Result |
++ | Increment | ++a | Increases a by 1 |
-- | Decrement | --a | Decreases a by 1 |
Example in Java
public class IncrementDecrementExample {
public static void main(String[] args) {
int a = 5;
System.out.println("a++: " + (a++)); // Output: 5 (then a becomes 6)
System.out.println("++a: " + (++a)); // Output: 7
}
}
Ternary Operator
The ternary operator is a shorthand for if-else
statements:
condition ? value_if_true : value_if_false
Example in Java
public class TernaryExample {
public static void main(String[] args) {
int a = 10, b = 5;
String result = (a > b) ? "a is greater" : "b is greater";
System.out.println(result); // Output: a is greater
}
}
Conclusion
Operators in Java provide the tools to perform calculations, make decisions, and manipulate data. Understanding these operators and their behavior is key to writing efficient code. Whether you're doing arithmetic, comparing values, or working with binary, these examples should have you covered. Keep practicing, and you'll be a pro in no time!
Subscribe to my newsletter
Read articles from Ajink Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Ajink Gupta
Ajink Gupta
I am an engineering student pursuing a degree in Artificial Intelligence and Data Science at Datta Meghe College of Engineering. I have strong technical skills in Full Stack Web Development, as well as programming in Python and Java. I currently manage Doubtly's blog and am exploring job opportunities as an SDE. I am passionate about learning new technologies and contributing to the tech community.