What Are Operators in Java?
Operators perform operations on variables and values. For example, adding two numbers, comparing two values, or combining conditions.
Types of Java Operators
1️⃣ Arithmetic Operators (for mathematical operations)
Operator | Description | Example |
---|---|---|
+ | Addition | int sum = 5 + 10; |
- | Subtraction | int diff = 10 - 5; |
* | Multiplication | int product = 5 * 2; |
/ | Division | int division = 10 / 2; |
% | Modulus (remainder) | int remainder = 10 % 3; |
++ | Increment by 1 | i++; |
– | Decrement by 1 | i--; |
2️⃣ Relational Operators (compare two values)
Operator | Description | Example |
---|---|---|
== | Equal | if (a == b) { } |
!= | Not equal | if (a != b) { } |
> | Greater than | if (a > b) { } |
< | Less than | if (a < b) { } |
>= | Greater than or equal | if (a >= b) { } |
<= | Less than or equal | if (a <= b) { } |
3️⃣ Logic Operators (combine two or more conditions)
Operator | Description | Example |
---|---|---|
&& | AND (both true) | if (a == 5 && b == 10) { } |
|| | OR (at least 1 true) | if (a == 5 \|\| b == 10) { } |
! | NOT (reverse boolean) | if (!isValid) { } |
4️⃣ Assignment Operators (assign or modify values)
Operator | Description | Example |
---|---|---|
= | Assign | int a = 5; |
+= | Add and assign | a += 5; // a = a + 5; |
-= | Subtract and assign | a -= 5; // a = a - 5; |
*= | Multiply and assign | a *= 5; // a = a * 5; |
/= | Divide and assign | a /= 5; // a = a / 5; |
%= | Modulus and assign | a %= 5; // a = a % 5; |
5️⃣ Bitwise Operators (work at bit level)
Operator | Description | Example |
---|---|---|
& | Bitwise AND | c = a & b; |
| | Bitwise OR | `c = a |
^ | Bitwise XOR | c = a ^ b; |
~ | Bitwise NOT | c = ~a; |
<< | Left shift | c = a << 2; |
>> | Right shift | c = a >> 2; |
Summary
- Arithmetic — Perform mathematical operations
- Relational — Compare two values
- Logic — Combine multiple conditions
- Assignment — Store or modify a variable’s value
- Bitwise — Perform operations at the bit level
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Terms Disclaimer About Us Contact Us
Copyright 2023-2025 © All rights reserved.