Operators are special symbols that perform operations on variables and values. These operations can range from mathematical calculations to logical comparisons and assignment of values. Java operators are classified into several categories, each serving specific functions.
Arithmetic Operators
Arithmetic operators in Java are used for basic mathematical operations, such as addition, subtraction, multiplication, division, and calculating remainders. These operators work with numeric data types like int
, float
, double
, etc.
Operator | Description | Example |
---|---|---|
+ | Addition: Adds two operands. | int sum = 5 + 3; // sum = 8 |
– | Subtraction: Subtracts second operand from first. | int diff = 10 – 4; // diff = 6 |
* | Multiplication: Multiplies two operands. | int product = 6 * 7; // product = 42 |
/ | Division: Divides the first operand by the second. | int quotient = 20 / 4; // quotient = 5 |
% | Modulo: Returns remainder of division. | int remainder = 10 % 3; // remainder = 1 |
Relational Operators
Relational operators are used to compare two values. These operators return a boolean result (true
or false
) depending on the comparison between the operands. They are typically used in conditional expressions, such as if
statements.
Operator | Description | Example |
---|---|---|
== | Equal to: Returns true if operands are equal. |
boolean result = (5 == 5); // true |
!= | Not equal to: Returns true if operands are not equal. |
boolean result = (5 != 3); // true |
> | Greater than: Returns true if the first operand is greater than the second. |
boolean result = (7 > 3); // true |
< | Less than: Returns true if the first operand is less than the second. |
boolean result = (3 < 7); // true |
>= | Greater than or equal to: Returns true if the first operand is greater or equal to the second. |
boolean result = (5 >= 5); // true |
<= | Less than or equal to: Returns true if the first operand is less or equal to the second. |
boolean result = (3 <= 5); // true |
Logical Operators
Logical operators are used to combine multiple boolean expressions or to invert boolean values. They are most often used in control flow statements like if
, while
, and for
.
Operator | Description | Example |
---|---|---|
&& | Logical AND: Returns true if both operands are true . |
boolean result = (true && false); // false |
|| | Logical OR: Returns true if at least one operand is true . |
boolean result = (true || false); // true |
! | Logical NOT: Inverts the boolean value. | boolean result = !(true); // false |
Assignment Operators
Assignment operators in Java are used to assign values to variables. They can also combine assignment with arithmetic or bitwise operations.
Operator | Description | Example |
---|---|---|
= | Simple assignment: Assigns the right operand to the left operand. | int a = 10; // a = 10 |
+= | Add and assign: Adds the right operand to the left operand and assigns the result. | a += 5; // a = a + 5 |
-= | Subtract and assign: Subtracts the right operand from the left operand and assigns the result. | a -= 3; // a = a – 3 |
*= | Multiply and assign: Multiplies the left operand by the right operand and assigns the result. | a *= 2; // a = a * 2 |
/= | Divide and assign: Divides the left operand by the right operand and assigns the result. | a /= 2; // a = a / 2 |
Unary Operators
Unary operators in Java operate on a single operand and perform operations like incrementing or decrementing the operand’s value, or negating it. These are often used to manipulate variables within loops or expressions.
Operator | Description | Example |
---|---|---|
++ | Increment: Increases the value of the operand by 1. | int a = 5; a++; // a = 6 |
— | Decrement: Decreases the value of the operand by 1. | int a = 5; a–; // a = 4 |
+ | Unary plus: Returns the operand as is (used mainly for positive numbers). | int a = +5; // a = 5 |
– | Unary minus: Negates the operand (makes it negative). | int a = -5; // a = -5 |
Ternary Operator
The ternary operator is a shorthand for if-else
statements. It evaluates a boolean expression and returns one of two values based on whether the condition is true
or false
.
Operator | Description | Example |
---|---|---|
?: | Ternary conditional: A shorthand for if-else statements. |
int result = (a > b) ? a : b; // result is the greater of a or b |