Operators in Java
In Java, operators are symbols used to perform operations on variables and values.
They help us calculate, compare, combine conditions, and assign or update data.
1. Arithmetic Operators
Used to perform basic mathematical calculations on numeric data types like int, float,
double, etc. They help us add, subtract, multiply, divide, and find remainders.
Operator What it does Example
+ Adds numbers a+b
- Subtracts numbers a-b
Multiplies numbers a*b
/ Divides numbers (quotient) a/b
% Finds remainder after division a % b
Tiny code example:
1 public class Arithm eticEx ample {
2 public static void main ( String [] args ) {
3 int a = 10 , b = 3;
4 System . out . println ( a + b ) ; // 13
5 System . out . println ( a - b ) ; // 7
6 System . out . println ( a * b ) ; // 30
7 System . out . println ( a / b ) ; // 3
8 System . out . println ( a % b ) ; // 1
9 }
10 }
2. Relational (Comparison) Operators
Used to compare two values and decide their relationship. They always return true or
false.
Operator Meaning Example (a=10, b=3)
== Equal to a == b → false
!= Not equal to a != b → true
> Greater than a > b → true
< Less than a < b → false
>= Greater than or equal to a >= b → true
<= Less than or equal to a <= b → false
Tiny code example:
1
1 public class Relati onalEx ample {
2 public static void main ( String [] args ) {
3 int a = 10 , b = 3;
4 System . out . println ( a > b ) ; // true
5 System . out . println ( a == b ) ; // false
6 }
7 }
3. Logical Operators
Used to combine, check, or reverse multiple boolean conditions. They help write complex
conditions in if, while, etc.
Operator Meaning Example
&& Logical AND (true if both are true) (a > 5 && b < 5) → true
|| Logical OR (true if any one is true) (a > 5 || b > 5) → true
! Logical NOT (reverses true/false) !(a > 5) → false
Tiny code example:
1 public class LogicalExample {
2 public static void main ( String [] args ) {
3 int a = 10 , b = 3;
4 System . out . println ( a > 5 && b < 5) ; // true
5 System . out . println ( a < 5 || b < 5) ; // true
6 System . out . println (!( a > 5) ) ; // false
7 }
8 }
4. Assignment Operators
Used to assign or update the value of a variable quickly and clearly. They save time and
keep code shorter.
Operator Meaning Example
= Simple assignment int x = 5;
+= Add and assign x += 2; → x = x+2;
-= Subtract and assign x -= 1; → x = x-1;
= Multiply and assign x *= 3; → x = x*3;
/= Divide and assign x /= 2; → x = x/2;
%= Modulus and assign x %= 2; → x = x%2;
Tiny code example:
2
1 public class Assign mentEx ample {
2 public static void main ( String [] args ) {
3 int x = 5;
4 x += 3; // now x is 8
5 System . out . println ( x ) ;
6 }
7 }
5. Summary Table of Types
Type of operator Used for Examples
Arithmetic Basic math on numbers +, -, *, /, %
(int, float, etc.)
Relational Compare two values and de- ==, !=, >, <, >=, <=
cide relationship
Logical Combine, check, or reverse &&, ||, !
conditions
Assignment Assign or update variable =, +=, -=, *=, /=, %=
values clearly