Operators are special symbols used to perform operations on variables and values. Java supports arithmetic, relational, logical, assignment, unary, bitwise, and other operators.
Operators perform operations such as addition, subtraction, comparison, and logical checking.
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 5;
System.out.println(a + b);
System.out.println(a > b);
}
}
15
true
Here, + adds two numbers, while
> checks whether the first number is
greater than the second.
Arithmetic operators perform mathematical calculations.
| Operator | Operation | Example | Result |
|---|---|---|---|
| + | Addition | 10 + 5 | 15 |
| - | Subtraction | 10 - 5 | 5 |
| * | Multiplication | 10 * 5 | 50 |
| / | Division | 10 / 5 | 2 |
| % | Remainder | 10 % 3 | 1 |
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 3;
System.out.println("Addition: " + (a + b));
System.out.println("Subtraction: " + (a - b));
System.out.println("Multiplication: " + (a * b));
System.out.println("Division: " + (a / b));
System.out.println("Remainder: " + (a % b));
}
}
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3
Remainder: 1
10 / 3 gives 3, not
3.333....
Assignment operators assign values to variables or update their existing values.
| Operator | Example | Equivalent operation |
|---|---|---|
| = | a = 10 | Assign 10 to a |
| += | a += 5 | a = a + 5 |
| -= | a -= 5 | a = a - 5 |
| *= | a *= 2 | a = a * 2 |
| /= | a /= 2 | a = a / 2 |
| %= | a %= 3 | a = a % 3 |
public class Main {
public static void main(String[] args) {
int a = 10;
a += 5;
System.out.println(a);
a -= 3;
System.out.println(a);
a *= 2;
System.out.println(a);
a /= 4;
System.out.println(a);
}
}
15
12
24
6
Relational operators compare two values. Their
expressions produce either true or
false.
| Operator | Meaning | Example |
|---|---|---|
| == | Equal to | 10 == 10 → true |
| != | Not equal to | 10 != 5 → true |
| > | Greater than | 10 > 5 → true |
| < | Less than | 5 < 10 → true |
| >= | Greater than or equal to | 10 >= 10 → true |
| <= | Less than or equal to | 5 <= 10 → true |
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 5;
System.out.println(a == b);
System.out.println(a != b);
System.out.println(a > b);
System.out.println(a < b);
System.out.println(a >= 10);
System.out.println(b <= 5);
}
}
false
true
true
false
true
true
= with ==.
The first assigns a value; the second compares values.
Logical operators combine or reverse Boolean expressions. They are commonly used in conditions.
| Operator | Name | Meaning |
|---|---|---|
| && | Logical AND | True when both conditions are true |
| || | Logical OR | True when at least one condition is true |
| ! | Logical NOT | Reverses a Boolean value |
public class Main {
public static void main(String[] args) {
int age = 20;
boolean hasID = true;
System.out.println(age >= 18 && hasID);
System.out.println(age < 18 || hasID);
System.out.println(!hasID);
}
}
true
true
false
| A | B | A && B | A || B |
|---|---|---|---|
| true | true | true | true |
| true | false | false | true |
| false | true | false | true |
| false | false | false | false |
Unary operators work with one operand. Examples include increment, decrement, unary plus, unary minus, and logical NOT.
| Operator | Meaning |
|---|---|
| ++ | Increment by one |
| -- | Decrement by one |
| + | Unary plus |
| - | Unary minus |
| ! | Logical NOT |
public class Main {
public static void main(String[] args) {
int a = 5;
a++;
System.out.println(a);
a--;
System.out.println(a);
System.out.println(-a);
System.out.println(!true);
}
}
6
5
-5
false
The increment operator increases a variable by one, while the decrement operator decreases it by one.
int a = 5;
System.out.println(a++);
System.out.println(a);
5
6
With a++, the current value is used in the
expression first, and then the variable is increased.
int b = 5;
System.out.println(++b);
System.out.println(b);
6
6
With ++b, the variable is increased first,
and then the updated value is used.
The ternary operator is a short form of an
if-else expression.
Syntax:
condition ? valueIfTrue : valueIfFalse;
public class Main {
public static void main(String[] args) {
int age = 20;
String result = (age >= 18)
? "Eligible to vote"
: "Not eligible to vote";
System.out.println(result);
}
}
Eligible to vote
If the condition is true, the first value is selected. Otherwise, the second value is selected.
Bitwise operators perform operations on the individual bits of integer values.
| Operator | Name |
|---|---|
| & | Bitwise AND |
| | | Bitwise OR |
| ^ | Bitwise XOR |
| ~ | Bitwise complement |
| << | Left shift |
| >> | Signed right shift |
| >>> | Unsigned right shift |
public class Main {
public static void main(String[] args) {
int a = 5;
int b = 3;
System.out.println(a & b);
System.out.println(a | b);
System.out.println(a ^ b);
System.out.println(a << 1);
}
}
1
7
6
10
For positive integers, 5 is 0101 and 3 is
0011 in four-bit binary notation.
Bitwise AND keeps only the bits that are set in both
numbers.
The instanceof operator checks whether an
object is an instance of a particular class or a
compatible type.
public class Main {
public static void main(String[] args) {
String name = "Rahul";
System.out.println(name instanceof String);
}
}
true
The variable name refers to a String
object, so the result is true.
Operator precedence determines which operation is evaluated first in an expression.
public class Main {
public static void main(String[] args) {
int result1 = 10 + 5 * 2;
int result2 = (10 + 5) * 2;
System.out.println(result1);
System.out.println(result2);
}
}
20
30
In the first expression, multiplication is performed before addition. In the second expression, parentheses make addition happen first.
()++, --, !* / %+ -< > <= >=== !=&&||=, +=, etc.
Java's logical AND (&&) and logical
OR (||) operators use short-circuit
evaluation.
&&, if the first condition
is false, Java does not evaluate the second one.
||, if the first condition is true,
Java does not evaluate the second one.
public class Main {
public static void main(String[] args) {
int a = 5;
int b = 0;
boolean result = (a < 0) && (10 / b > 1);
System.out.println(result);
}
}
false
The first condition is false, so Java skips the second condition. This prevents division by zero in this example.
&& and ||.
The Boolean operators & and
| do not short-circuit when used with
Boolean operands.
This program uses arithmetic, relational, logical, and ternary operators to check a student's result.
public class Main {
public static void main(String[] args) {
int english = 75;
int computer = 85;
int mathematics = 65;
int total = english + computer + mathematics;
double average = total / 3.0;
boolean passed =
english >= 40 &&
computer >= 40 &&
mathematics >= 40;
String result = passed ? "Pass" : "Fail";
System.out.println("Total Marks: " + total);
System.out.printf("Average: %.2f%n", average);
System.out.println("Result: " + result);
}
}
Total Marks: 225
Average: 75.00
Result: Pass
Q1. What is the result of 10 % 3?
Q2. Which operator means logical AND?
Q3. What is the result of 10 + 5 * 2?