Operators in Java
Learn how Java operators are used to perform arithmetic, comparison, logical, assignment, bitwise, and conditional operations.
What is an Operator?
An operator is a symbol that tells Java to perform an operation on one or more values or variables.
int a = 10;
int b = 5;
int result = a + b;
Here, + is the arithmetic operator.
Types of Operators
| Operator Type | Examples |
|---|---|
| Arithmetic | + - * / % |
| Unary | ++ -- + - ! ~ |
| Relational | == != > < >= <= |
| Logical | && || ! |
| Assignment | = += -= *= /= %= |
| Bitwise | & | ^ ~ |
| Shift | << >> >>> |
| Conditional | ? : |
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
| Operator | Meaning | Example |
|---|---|---|
+ |
Addition | a + b |
- |
Subtraction | a - b |
* |
Multiplication | a * b |
/ |
Division | a / b |
% |
Remainder | a % b |
int a = 20;
int b = 6;
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);
Addition with Strings
The + operator also performs string concatenation
when one operand is a String.
String name = "CIIT Institute 🤓 ";
int startingYear = 2014;
System.out.println(name + " Established in " + startingYear);
Output:
CIIT Institute 🤓 Established in 2014
2. Unary Operators
Unary operators operate on a single operand.
| Operator | Meaning |
|---|---|
+ |
Positive value |
- |
Negates a numeric value |
++ |
Increment by one |
-- |
Decrement by one |
! |
Logical negation |
~ |
Bitwise complement |
int count = 10;
count++;
System.out.println(count);
count--;
System.out.println(count);
Pre-Increment and Post-Increment
The position of ++ determines whether the value
is incremented before or after its current value is used.
int a = 5;
int x = ++a;
System.out.println(a);
System.out.println(x);
In pre-increment, the variable is incremented before the
value is assigned to x.
int b = 5;
int y = b++;
System.out.println(b);
System.out.println(y);
In post-increment, the current value is used first and then the variable is incremented.
3. Relational Operators
Relational operators compare two values and produce a boolean result.
| Operator | Meaning |
|---|---|
== |
Equal to |
!= |
Not equal to |
> |
Greater than |
< |
Less than |
>= |
Greater than or equal to |
<= |
Less than or equal to |
int age = 25;
System.out.println(age >= 18);
System.out.println(age == 25);
System.out.println(age < 18);
== with Objects and Strings
For primitive values, == compares values.
For object references, == checks whether two
references refer to the same object.
To compare String contents, use equals().
String a = new String("CIIT");
String b = new String("CIIT");
System.out.println(a == b);
System.out.println(a.equals(b));
equals() when comparing the contents of
String objects.
4. Logical Operators
Logical operators combine boolean expressions.
| Operator | Meaning |
|---|---|
&& |
Logical AND |
|| |
Logical OR |
! |
Logical NOT |
int age = 25;
boolean hasId = true;
boolean eligible = age >= 18 && hasId;
System.out.println(eligible);
Short-Circuit Evaluation
Java's && and || operators
use short-circuit evaluation.
With &&, if the first condition is false,
the remaining conditions may not be evaluated. With
||, if the first condition is true, the remaining
conditions may not be evaluated.
int age = 15;
if (age >= 18 && age <= 60) {
System.out.println("Eligible");
}
5. Assignment Operators
Assignment operators assign values to variables.
| Operator | Equivalent |
|---|---|
= |
a = b |
+= |
a = a + b |
-= |
a = a - b |
*= |
a = a * b |
/= |
a = a / b |
%= |
a = a % b |
int value = 10;
value += 5;
value *= 2;
System.out.println(value);
6. Bitwise Operators
Bitwise operators operate on the individual bits of integral values.
| Operator | Meaning |
|---|---|
& |
Bitwise AND |
| |
Bitwise OR |
^ |
Bitwise XOR |
~ |
Bitwise complement |
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);
7. Shift Operators
Shift operators move the bits of an integral value.
| Operator | Meaning |
|---|---|
<< |
Left shift |
>> |
Signed right shift |
>>> |
Unsigned right shift |
int number = 8;
System.out.println(number << 1);
System.out.println(number >> 1);
8. Ternary Operator
The ternary operator is a compact conditional expression with three operands.
int age = 20;
String result = age >= 18 ? "Adult" : "Minor";
System.out.println(result);
Syntax:
condition ? valueIfTrue : valueIfFalse
Operator Precedence
When an expression contains multiple operators, Java follows defined precedence and associativity rules.
int result = 10 + 5 * 2;
System.out.println(result);
Multiplication has higher precedence than addition, so the multiplication is evaluated first.
Use parentheses when you want the evaluation order to be explicit.
int result = (10 + 5) * 2;
System.out.println(result);
Complete Example
public class Operators {
public static void main(String[] args) {
int marks = 85;
int bonus = 5;
int total = marks + bonus;
boolean passed = total >= 40;
String result = passed ? "Pass" : "Fail";
System.out.println("Total: " + total);
System.out.println("Result: " + result);
}
}
Operator Best Practices
- Use parentheses when an expression could be difficult to understand.
- Avoid unnecessarily complex expressions.
- Remember that integer division discards the fractional part.
-
Use
equals()for String content comparison. - Use short-circuit operators when appropriate for boolean conditions.
- Be careful with overflow when performing arithmetic on fixed-width integer types.
Summary
Java provides a rich set of operators for arithmetic, comparison, logical operations, assignment, bitwise manipulation, shifting, and conditional expressions. Understanding operator behavior and precedence is essential for writing correct Java expressions.