java
- IntroductionInstallation and SetupJava Syntax and StructureRunning Your First ProgramJava VariablesData Types in JavaOperators in JavaJava Input and OutputControl StatementsLoops in JavaMethods in Java (Functions)Arrays in JavaString Handling in JavaOOPS Concept in JavaClasses and ObjectsConstructor and Constructor Overloadingthis KeywordStatic MembersInheritanceAggregation in JavaMethod OverloadingMethod Overridingsuper KeywordFinal Keyword with Class, Method, and VariablesAccess Modifiers in javaEncapsulation in Java Polymorphism in JavaAbstraction in JavaAbstract ClassesInterfaces in JavaDifference between Abstract Class and Interface Nested and Inner Classes Exception HandlingJava PackagesWrapper Classes and AutoboxingJava Collections FrameworkFile Handling in JavaMultithreadingBasics of Java Memory Management and Garbage CollectionJava JDBC
Java Operators Tutorial for Beginners
Last Updated on: 19th Oct 2025 22:03:44 PM
Welcome to this beginner-friendly tutorial on Operators in Java! Operators are special symbols that perform operations on variables and values, like adding numbers or comparing values. They are essential for writing Java programs, as they help you manipulate data and make decisions.
This tutorial is designed for absolute beginners. We'll explore the types of operators in Java, explain each one clearly, and provide simple, real-world examples. You'll also find runnable code snippets you can try in any Java environment (e.g., Eclipse, IntelliJ, or online compilers like Repl.it).
By the end, you'll understand how to use operators effectively. Let’s dive in!
What Are Operators?
Operators are like tools that work on operands (values or variables) to produce a result. For example:
-
+ adds two numbers (e.g., 2 + 3 = 5).
-
== checks if two values are equal (e.g., 5 == 5 is true).
Types of Operators in Java:
-
Arithmetic Operators
-
Relational Operators
-
Logical Operators
-
Bitwise Operators
-
Assignment Operators
-
Miscellaneous Operators (Ternary, instanceof)
Beginner Tip: Always declare variables before using them, e.g., int x = 10; creates an integer variable x with value 10.
1. Arithmetic Operators
These operators handle basic math operations, working with numbers (integers or decimals).
|
Operator |
Description |
Example |
|---|---|---|
|
+ |
Addition |
5 + 3 = 8 |
|
- |
Subtraction |
5 - 3 = 2 |
|
* |
Multiplication |
5 * 3 = 15 |
|
/ |
Division |
5 / 2 = 2 (integer division) |
|
% |
Modulus (remainder) |
5 % 2 = 1 |
|
++ |
Increment (adds 1) |
x++ or ++x |
|
-- |
Decrement (subtracts 1) |
x-- or --x |
Explanation
-
Division: When dividing integers, the result is an integer (e.g., 5 / 2 = 2, not 2.5). For decimals, use floats (e.g., 5.0 / 2.0 = 2.5).
-
Increment/Decrement:
-
x++ (post-increment): Uses x, then adds 1.
-
++x (pre-increment): Adds 1, then uses x.
-
Similarly for --.
-
Example Code
Here’s a program to try arithmetic operators:
public class ArithmeticExample {
public static void main(String[] args) {
int a = 10;
int b = 3;
System.out.println("Addition: " + (a + b)); // 13
System.out.println("Subtraction: " + (a - b)); // 7
System.out.println("Multiplication: " + (a * b)); // 30
System.out.println("Division: " + (a / b)); // 3
System.out.println("Modulus: " + (a % b)); // 1
a++; // a becomes 11
System.out.println("After Increment: " + a); // 11
b--; // b becomes 2
System.out.println("After Decrement: " + b); // 2
}
}
How to Run: Save as ArithmeticExample.java, compile with javac ArithmeticExample.java, and run with java ArithmeticExample.
Real-World Analogy: Think of these as buttons on a calculator for adding scores, splitting bills, or counting items.
2. Relational Operators
These compare two values and return a boolean (true or false). They’re used in conditions, like in if statements.
|
Operator |
Description |
Example |
|---|---|---|
|
== |
Equal to |
5 == 5 → true |
|
!= |
Not equal to |
5 != 3 → true |
|
> |
Greater than |
5 > 3 → true |
|
< |
Less than |
5 < 3 → false |
|
>= |
Greater than or equal to |
5 >= 5 → true |
|
<= |
Less than or equal to |
5 <= 3 → false |
Explanation
-
Always return true or false.
-
Often used to control program flow (e.g., in if conditions).
Example Code
public class RelationalExample {
public static void main(String[] args) {
int x = 8;
int y = 5;
System.out.println("x == y: " + (x == y)); // false
System.out.println("x != y: " + (x != y)); // true
System.out.println("x > y: " + (x > y)); // true
System.out.println("x < y: " + (x < y)); // false
System.out.println("x >= y: " + (x >= y)); // true
System.out.println("x <= y: " + (x <= y)); // false
}
}
Real-World Analogy: Like checking if you have enough money to buy a toy (money >= price).
3. Logical Operators
In Java, Logical Operators are used to combine two or more boolean expressions (conditions) and return a boolean result — either true or false.
They are mainly used in conditional statements like if, while, and for.
|
Operator |
Description |
Example |
|
|---|---|---|---|
|
&& |
Logical AND (true if both are true) |
(true && false) → false |
|
|
|| |
|
true || false -> true |
|
|
! |
Logical NOT (flips the value) |
!true → false |
1. Logical AND (&&)
-
Both conditions must be true for the result to be
true. -
It’s a short-circuit operator (stops checking if the first condition is false).
Example Code :
int a = 10, b = 20;
if (a > 5 && b > 15) {
System.out.println("Both conditions are true");
} else {
System.out.println("At least one condition is false");
}
Output:
Both conditions are true
2. Logical OR (||)
-
Returns
trueif at least one condition is true. -
Also a short-circuit operator (stops checking if the first is true).
Example Code :
int a = 10, b = 5;
if (a > 8 || b > 10) {
System.out.println("At least one condition is true");
} else {
System.out.println("Both conditions are false");
}
Output:
At least one condition is true
3. Logical NOT (!)
-
Reverses a boolean value.
-
If condition is
true, it becomesfalse, and vice versa.
Example Code :
boolean isJavaFun = true;
if (!isJavaFun) {
System.out.println("Java is not fun");
} else {
System.out.println("Java is fun!");
}
Output:
Java is fun!
4. Bitwise Operators
These operate on the binary (0s and 1s) representation of numbers. They’re advanced but useful for tasks like flags or encryption.
List of Bitwise Operators
| Operator | Name | Description | Example |
|---|---|---|---|
& |
Bitwise AND | 1 if both bits are 1 | a & b |
| | | Bitwise OR | Bitwise OR | 1 if any one bit is 1 |
^ |
Bitwise XOR | 1 if bits are different | a ^ b |
~ |
Bitwise NOT (Complement) | Inverts bits (1 → 0, 0 → 1) | ~a |
<< |
Left Shift | Shifts bits to the left, fills with 0 | a << 2 |
>> |
Right Shift | Shifts bits to the right, keeps sign bit | a >> 2 |
>>> |
Unsigned Right Shift | Shifts bits right, fills with 0 (no sign bit) | a >>> 2 |
Example Code :
public class BitwiseExample {
public static void main(String[] args) {
int a = 5; // Binary: 101
int b = 3; // Binary: 011
System.out.println("AND: " + (a & b)); // 1 (001)
System.out.println("OR: " + (a | b)); // 7 (111)
System.out.println("XOR: " + (a ^ b)); // 6 (110)
System.out.println("NOT a: " + (~a)); // -6
System.out.println("Left Shift: " + (a << 1)); // 10
System.out.println("Right Shift: " + (a >> 1)); // 2
}
}
Beginner Tip: Bitwise operators are advanced. Focus on arithmetic/relational first unless you need low-level operations.
5. Assignment Operators
Assignment operators are used to assign values to variables.
The basic assignment operator is =, and Java also provides compound (shorthand) assignment operators for quick calculations and assignments.
List of Assignment Operators in Java
|
Operator |
Description |
Example (Equivalent) |
|---|---|---|
|
= |
Assigns value |
a = 5 |
|
+= |
Adds and assigns |
a += 3 (a = a + 3) |
|
-= |
Subtracts and assigns |
a -= 3 (a = a - 3) |
|
*= |
Multiplies and assigns |
a *= 3 (a = a * 3) |
|
/= |
Divides and assigns |
a /= 3 (a = a / 3) |
|
%= |
Modulus and assigns |
a %= 3 (a = a % 3) |
Explanation
-
Shortcuts for updating variables efficiently.
Example Program :
public class AssignmentExample {
public static void main(String[] args) {
int a = 10, b = 5;
a += b; // a = 10 + 5
System.out.println("a += b ? " + a); // 15
a -= b; // a = 15 - 5
System.out.println("a -= b ? " + a); // 10
a *= b; // a = 10 * 5
System.out.println("a *= b ? " + a); // 50
a /= b; // a = 50 / 5
System.out.println("a /= b ? " + a); // 10
a %= b; // a = 10 % 5
System.out.println("a %= b ? " + a); // 0
}
}
Output :
a += b ? 15
a -= b ? 10
a *= b ? 50
a /= b ? 10
a %= b ? 0
6. Miscellaneous Operators
These are unique operators that don’t fit other categories.
-
Ternary Operator (? :): A compact if-else. Format: condition ? valueIfTrue : valueIfFalse.
-
instanceof: Checks if an object is of a specific type.
Example Code for Ternary
public class TernaryExample {
public static void main(String[] args) {
int age = 16;
String result = (age >= 18) ? "Can vote" : "Cannot vote";
System.out.println(result); // Cannot vote
}
}
Example Code for instanceof
public class InstanceofExample {
public static void main(String[] args) {
String name = "Alice";
System.out.println("Is name a String? " + (name instanceof String)); // true
}
}
Real-World Analogy: Ternary is like choosing between two snacks based on hunger: hungry ? "Pizza" : "Apple".
Operator Precedence
Operators have a priority order. For example:
-
* and / are evaluated before + and -.
-
Use () to control order, e.g., 2 + 3 * 4 = 14, but (2 + 3) * 4 = 20.
Summary
You’ve learned the six types of Java operators:
-
Arithmetic: Math operations (+, -, *, /, %, ++, --).
-
Relational: Comparisons (==, !=, >, <, >=, <=).
-
Logical: Combining booleans (&&, ||, !).
-
Bitwise: Binary operations (&, |, ^, ~, <<, >>, >>>).
-
Assignment: Assigning values (=, +=, -=, etc.).
-
Miscellaneous: Ternary (? :) and instanceof.
Keep practicing, and you’ll master operators in no time! Happy coding! ![]()
.png)