The Java operators are special symbols used to perform operations on variables & values. The Operators are fundamental to Java programming and are frequently asked in interviews.
What Are Operators?
The Operators are symbols that tell the compiler to perform
specific mathematical, relational, logical or assignment operations.
Example :
int a = 10;
int b = 5;
int sum = a + b; // + is an operator
System.out.println(sum);
Output:
15
Types of Operators in Java
- Arithmetic
Operators
- Unary
Operators
- Assignment
Operators
- Relational
Operators
- Logical
Operators
- Bitwise
Operators
- Ternary Operator
1. Arithmetic Operators
The Arithmetic Operators in Java are used to perform the basic mathematical calculations on numeric values. They help in operations such as addition, subtraction, multiplication, division & finding remainders.
|
Operator |
Description |
|
+ |
Addition |
|
- |
Subtraction |
|
* |
Multiplication |
|
/ |
Division |
|
% |
Modulus (Remainder) |
Example :
public class Main {
public static void main(String[]
args) {
int a = 20, b = 4;
System.out.println(a + b);
System.out.println(a - b);
System.out.println(a * b);
System.out.println(a / b);
System.out.println(a % b);
}
}
Output :
24
16
80
5
0
2. Unary Operators
The Unary Operators in Java operate on a single operand to perform actions such as incrementing, decrementing a value. They are commonly used for modifying variables & boolean expressions.
|
Operator |
Description |
|
+ |
Positive |
|
- |
Negative |
|
++ |
Increment |
|
-- |
Decrement |
|
! |
Logical NOT |
Example :
int x = 5;
System.out.println(++x);
System.out.println(--x);
Output :
6
5
Pre-Increment vs Post-Increment
Pre-Increment :
int a = 5;
System.out.println(++a);
Output:
6
Post-Increment :
int a = 5;
System.out.println(a++);
System.out.println(a);
Output:
5
6
3. Assignment Operators
The Assignment Operators in Java are used to assign values to variables & perform operations while assigning.
|
Operator |
Example |
|
= |
a = 10 |
|
+= |
a += 5 |
|
-= |
a -= 5 |
|
*= |
a *= 5 |
|
/= |
a /= 5 |
|
%= |
a %= 5 |
Example :
int a = 10;
a += 5;
System.out.println(a);
Output:
15
4. Relational Operators
The Relational Operators in Java are used to compare two values or variables. They return a boolean result (true or false) based on the comparison condition.
Operator | Meaning |
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal |
<= | Less than or equal |
Example :
int a = 10;
int b = 20;
System.out.println(a < b);
System.out.println(a == b);
Output :
true
false
5. Logical Operators
The Logical Operators in Java are used to combine or evaluate multiple conditions. They return a boolean result (true or false) based on the logical relationship between expressions.
Operator | Description |
&& | Logical AND |
|| | Logical OR |
! | Logical NOT |
Example :
boolean x = true;
boolean y = false;
System.out.println(x && y);
System.out.println(x || y);
System.out.println(!x);
Output :
false
true
false
6. Bitwise Operators
The Bitwise Operators in Java perform operations directly on the binary representation of integer values. They manipulate individual bits & return a numeric result.
Operator | Description |
& | AND |
| | OR |
^ | XOR |
~ | NOT |
Example :
int a = 5;
int b = 3;
System.out.println(a & b);
System.out.println(a | b);
Output :
1
7
7. Ternary Operator
The Ternary Operator in Java simplifies conditional expressions by replacing simple if-else statements. It evaluates a condition & chooses between two possible values.
Syntax :
condition ? value1 : value2;
Example :
int num = 10;
String result = (num % 2 == 0) ? "Even" : "Odd";
System.out.println(result);
Output :
Even
Common Interview Questions
1. What is an operator in Java?
An operator is a symbol that performs operations on
variables and values.
2. Difference between ++i and i++?
++i (Pre Increment) :
The Value is incremented first and then used.
i++ (Post Increment) :
The Value is used first and then incremented.
3. What is the modulus operator?
% returns the remainder after division.
10 % 3
Output:
1
4. What is the ternary operator?
A compact form of if-else.
int max = (a > b) ? a : b;
5. What is operator precedence?
The order in which operators are evaluated in an expression.
Example:
int result = 5 + 2 * 3;
Output:
11
The Multiplication executes first.
6. What is the output?
int a = 5;
int b = a++ + ++a;
System.out.println(b);
Solution
- a++ -> 5
- a becomes 6
- ++a -> 7
Output:
12
7. What is the output?
System.out.println(10 + 20 + "Java");
Output:
30Java
8. What is the output?
System.out.println("Java" + 10 + 20);
Output:
Java1020
9. Can logical operators work with non-boolean values?
No. The Logical operators require boolean expressions.
10. What is the output?
int x = 10;
System.out.println(x++ + x++);
Output:
21
Explanation:
- First x++ = 10
- Second x++ = 11
- 10 + 11 = 21
Conclusion
The Java operators are essential building blocks that allow
developers to perform calculations, comparisons, logical decisions and data
manipulation efficiently. Understanding different types of operators helps in
writing clean & optimized code. Arithmetic, relational, logical, assignment
and bitwise operators are commonly used in everyday programming. A strong grasp
of operator precedence & increment/decrement is crucial for avoiding
logical errors. The Operators are frequently tested in coding interviews &
technical assessments. Mastering Java operators lays a solid foundation for
learning advanced Java concepts & developing real-world applications.