Java Operators
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations such as addition,
subtraction, multiplication, division, and modulus.
Operator Name Description
+ Addition Adds two operands.
- Subtraction Subtracts the second
operand from the first.
* Multiplication Multiplies two operands.
/ Division Divides the numerator by
the denominator.
% Modulus Returns the remainder of
division.
Example:
int a = 10, 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("Modulus: " + (a % b));
2. Relational Operators
Relational operators compare two values and return a boolean result (true or false).
Operator Name Description
== Equal to Checks if two values are
equal.
!= Not equal to Checks if two values are not
equal.
> Greater than Checks if the left value is
greater.
< Less than Checks if the left value is
smaller.
>= Greater than or equal to Checks if the left value is
greater or equal.
<= Less than or equal to Checks if the left value is
smaller or equal.
1
Example:
int a = 10, b = 20;
System.out.println("a == b: " + (a == b));
System.out.println("a != b: " + (a != b));
System.out.println("a > b: " + (a > b));
System.out.println("a < b: " + (a < b));
System.out.println("a >= b: " + (a >= b));
System.out.println("a <= b: " + (a <= b));
3. Logical Operators
Logical operators are used to combine multiple boolean expressions.
Operator Name Description
&& Logical AND Returns true if both
conditions are true.
|| Logical OR Returns true if at least one
condition is true.
! Logical NOT Inverts the boolean value.
Example:
boolean x = true, y = false;
System.out.println("x && y: " + (x && y));
System.out.println("x || y: " + (x || y));
System.out.println("!x: " + (!x));
4. Bitwise Operators (with 2's Complement)
Bitwise operators work at the bit level. They are used with integer data types. Java uses 2's
complement to represent negative numbers.
Operator Name Description
& Bitwise AND Sets each bit to 1 if both bits
are 1.
| Bitwise OR Sets each bit to 1 if one of
the two bits is 1.
^ Bitwise XOR Sets each bit to 1 if only one
of the two bits is 1.
~ Bitwise NOT Inverts all the bits (2's
complement for negative).
<< Left Shift Shifts bits to the left
2
(multiply by 2ⁿ).
>> Right Shift Shifts bits to the right
(divide by 2ⁿ, preserves
sign).
>>> Unsigned Right Shift Shifts bits right with zero
fill.
Example:
public class BitwiseDemo {
public static void main(String[] args) {
int a = 5; // Binary: 00000000 00000000 00000000 00000101
int b = 3; // Binary: 00000000 00000000 00000000 00000011
// Bitwise AND: 00000101 & 00000011 = 00000001 (1)
System.out.println("a & b = " + (a & b)); // Output: 1
// Bitwise OR: 00000101 | 00000011 = 00000111 (7)
System.out.println("a | b = " + (a | b)); // Output: 7
// Bitwise XOR: 00000101 ^ 00000011 = 00000110 (6)
System.out.println("a ^ b = " + (a ^ b)); // Output: 6
// Bitwise NOT: ~00000101 = 11111111 11111111 11111111
11111010 (2's complement → -6)
System.out.println("~a = " + (~a)); // Output: -6
// Left shift: 00000101 << 1 = 00001010 (10) — multiply by 2
System.out.println("a << 1 = " + (a << 1)); // Output: 10
// Right shift (arithmetic): 00000101 >> 1 = 00000010 (2) —
divide by 2
System.out.println("a >> 1 = " + (a >> 1)); // Output: 2
}}
3
5. Assignment Operators
Assignment operators are used to assign values to variables with or without performing
operations.
Operator Name Description
= Assignment Assigns the right-hand
value to the left-hand
variable.
+= Add and assign Adds and assigns.
-= Subtract and assign Subtracts and assigns.
*= Multiply and assign Multiplies and assigns.
/= Divide and assign Divides and assigns.
%= Modulus and assign Modulus and assigns.
Example:
int a = 10;
a += 5;
System.out.println("a after += 5: " + a);
a -= 3;
System.out.println("a after -= 3: " + a);
a *= 2;
System.out.println("a after *= 2: " + a);
a /= 4;
System.out.println("a after /= 4: " + a);
a %= 3;
System.out.println("a after %= 3: " + a);
6. Unary Operators
Unary operators work with a single operand. They are used to perform operations like
increment, decrement, and negation.
Operator Name Description
+ Unary plus Indicates a positive value.
- Unary minus Negates an expression.
++ Increment Increases a value by 1.
-- Decrement Decreases a value by 1.
! Logical NOT Inverts a boolean value.
4
Example:
int a = 5;
System.out.println("++a = " + (++a));
System.out.println("a++ = " + (a++));
System.out.println("--a = " + (--a));
System.out.println("a-- = " + (a--));
System.out.println("Final value of a: " + a);
7. Conditional (Ternary) Operator
The ternary operator provides a shorthand way to write simple if-else conditions.
Operator Name Description
?: Ternary Returns one of two values
based on a boolean
expression.
Example:
int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println("Maximum: " + max);