In Java, operators are special symbols or keywords used to perform operations on operands (variables or values). Understanding how to use operators is essential for performing calculations, making comparisons, and implementing logical conditions in your Java programs.
Here’s a breakdown of the different types of operators available in Java:
1. Arithmetic Operators
Arithmetic operators are used for basic mathematical calculations such as addition, subtraction, multiplication, and division.
| Operator | Description | Example |
|---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (Remainder) | a % b |
Example:
java
int a = 10, b = 5;
System.out.println(a + b); // 15
System.out.println(a - b); // 5
System.out.println(a * b); // 50
System.out.println(a / b); // 2
System.out.println(a % b); // 0
2. Relational (Comparison) Operators
These operators compare two values and return a boolean (true or false), helping in making decisions.
| Operator | Description | Example |
|---|---|---|
== | Equal to | a == b |
!= | Not equal to | a != b |
> | Greater than | a > b |
< | Less than | a < b |
>= | Greater than or equal to | a >= b |
<= | Less than or equal to | a <= b |
Example:
java
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 >= b); // true
System.out.println(a <= b); // false
3. Logical Operators
Logical operators are used to combine multiple conditions and return a boolean value.
| Operator | Description | Example |
|---|---|---|
&& | Logical AND | a > b && b < c |
| ` | ` | |
! | Logical NOT | !(a > b) |
Example:
java
boolean a = true, b = false;
System.out.println(a && b); // false
System.out.println(a || b); // true
System.out.println(!a); // false
4. Unary Operators
Unary operators operate on a single operand to perform operations like incrementing, decrementing, and negating values.
| Operator | Description | Example |
|---|---|---|
++ | Increment | a++ or ++a |
-- | Decrement | a-- or --a |
+ | Unary plus | +a |
- | Unary minus | -a |
! | Logical NOT | !a |
Example:
java
int a = 5;
System.out.println(++a); // 6
System.out.println(a--); // 6
System.out.println(-a); // -6
5. Assignment Operators
Assignment operators are used to assign values to variables, including shorthand operators for addition, subtraction, etc.
| Operator | Description | Example |
|---|---|---|
= | Assign value | a = b |
+= | Add and assign | a += b |
-= | Subtract and assign | a -= b |
*= | Multiply and assign | a *= b |
/= | Divide and assign | a /= b |
%= | Modulus and assign | a %= b |
Example:
java
int a = 10;
a += 5; // a = a + 5
System.out.println(a); // 15
6. Bitwise Operators
Bitwise operators operate on the binary representation of numbers and perform bit-by-bit operations.
| Operator | Description | Example |
|---|---|---|
& | AND | a & b |
| ` | ` | OR |
^ | XOR | a ^ b |
~ | NOT | ~a |
<< | Left shift | a << 2 |
>> | Right shift | a >> 2 |
>>> | Unsigned right shift | a >>> 2 |
Example:
java
int a = 5, b = 3;
System.out.println(a & b); // 1
System.out.println(a | b); // 7
System.out.println(a ^ b); // 6
7. Ternary Operator
The ternary operator is a shorthand way of writing if-else statements.
| Operator | Description | Example |
|---|---|---|
? : | Conditional (if-else) | condition ? value1 : value2 |
Example:
java
int a = 10, b = 5;
int max = (a > b) ? a : b;
System.out.println(max); // 10
8. Type Comparison Operator
This operator checks whether an object is an instance of a particular class or interface.
| Operator | Description | Example |
|---|---|---|
instanceof | Type comparison | obj instanceof MyClass |
Example:
java
String str = "Hello";
System.out.println(str instanceof String); // true
Reviewed by GABA TECHONOLGIES
on
March 10, 2025
Rating: 5
.png)
No comments