Java Operators

In Java, operators are special symbols or keywords used to perform operations on variables and values. They are essential in writing expressions and manipulating data in a program.

1️⃣. Arithmetic Operators

Arithmetic operators in Java are used to perform basic mathematical calculations(like addition, subtraction, multiplication, division, and modulus) on numeric data types like int, float, double, etc.
These operators form the foundation of most computations in programming.

OperatorDescriptionSyntaxExample
+Additiona + b5 + 3 = 8
Subtractiona - b5 - 3 = 2
*Multiplicationa * b5 * 3 = 15
/Divisiona / b10 / 2 = 5
%Modulus (Remainder)a % b10 % 3 = 1

✅ Example:

int a = 10, b = 3;
System.out.println(a + b);  // Output: 13
System.out.println(a - b);  // Output: 7
System.out.println(a * b);  // Output: 30
System.out.println(a / b);  // Output: 3
System.out.println(a % b);  // Output: 1

2️⃣. Relational (Comparison) Operators

Relational operators are used to compare two values and return a result of type boolean — either true or false.
They are essential when you want to make decisions in your code, such as in conditional statements (if, while, etc.).

OperatorDescriptionSyntaxExample
==Equal toa == b5 == 3 → false
!=Not equal toa != b5 != 3 → true
>Greater thana > b5 > 3 → true
<Less thana < b5 < 3 → false
>=Greater or equala >= b5 >= 3 → true
<=Less or equala <= b5 <= 3 → false

✅ Example:

int a = 5, b = 10;
System.out.println(a == b);  // false
System.out.println(a != b);  // true
System.out.println(a < b);   // true
System.out.println(a >= b);  // false

3️⃣. Logical Operators

Logical operators are used to combine multiple boolean expressions or conditions and return a result of type boolean (true or false).
They are especially useful when you need to make decisions based on multiple conditions in your program

OperatorDescriptionSyntaxExample Expression
&&Logical AND(a > b) && (b > c)true if both conditions are true
||Logical OR`(a > b)
!Logical NOT!(a > b)Inverts boolean value (true → false, false → true)

✅ Example:

int a = 5, b = 10, c = 3;
System.out.println((a < b) && (b > c));  // true
System.out.println((a > b) || (b > c));  // true
System.out.println(!(a > b));            // true

4️⃣. Assignment Operators

Assignment operators are used in Java to assign values to variables. They take the value on the right-hand side and store it in the variable on the left-hand side.

The simplest assignment operator is the = operator.

Syntax:

variable = value;

Example:

int a = 10; // assigns the value 10 to variable a

Here, 10 is assigned to a. The expression a = 10 not only assigns the value but also returns the assigned value (10), which can be used in other expressions.

OperatorDescriptionSyntaxExample
=Assigna = ba = 5;
+=Add and assigna += ba = a + b;
-=Subtract and assigna -= ba = a - b;
*=Multiply and assigna *= ba = a * b;
/=Divide and assigna /= ba = a / b;
%=Modulus and assigna %= ba = a % b;

✅ Example:

int a = 5;
a += 3;  // a = a + 3 → a becomes 8
System.out.println(a);  // Output: 8

5️⃣. Unary Operators

Unary operators are operators that operate on a single operand to produce a new value or change the state of a variable. They are typically used for incrementing, decrementing, negating, or inverting values.

Syntax:

operator operand;

or

operand operator;
OperatorDescriptionSyntaxExample
+Unary plus+a+5 → 5
Unary minus-a-5 → -5
++Increment by 1a++ / ++aIncrement a
Decrement by 1a-- / --aDecrement a
!Logical NOT!truefalse

✅ Example:

int a = 5;
System.out.println(++a);  // Output: 6 (pre-increment)
System.out.println(a--);  // Output: 6 (post-decrement; a becomes 5 after)

6️⃣. Ternary Operator

6.1. Ternary Operator

The ternary operator is a shortcut for the if-else statement.
It’s called “ternary” because it takes three operands.

Syntax:

variable = (condition) ? valueIfTrue : valueIfFalse;
  • condition → a boolean expression that evaluates to true or false.
  • valueIfTrue → value assigned if the condition is true.
  • valueIfFalse → value assigned if the condition is false.

It returns a value, unlike if-else, which is a statement.

6.2. How It Works

  1. Evaluate the condition.
  2. If the condition is true, the first value (valueIfTrue) is used.
  3. If the condition is false, the second value (valueIfFalse) is used.

✅ Example:

int a = 10;
int b = 20;

int max = (a > b) ? a : b;  // check which is larger
System.out.println("Maximum: " + max);

Output:

Maximum: 20

Explanation:

  • (a > b)10 > 20false
  • So valueIfFalse (b) is assigned to max.

6.3. Ternary Operator vs if-else

Using if-else:

int a = 10;
int b = 20;
int max;

if(a > b) {
    max = a;
} else {
    max = b;
}
System.out.println("Maximum: " + max);

Using Ternary Operator (shorter and cleaner):

int max = (a > b) ? a : b;

The ternary operator is concise, making it useful for simple conditional assignments.

6.4. Nested Ternary Operator

You can nest ternary operators to check multiple conditions, but be careful—it can reduce readability.

Example:

int a = 5, b = 10, c = 7;

int max = (a > b) ? a : (b > c ? b : c);
System.out.println("Maximum: " + max);

Output:

Maximum: 10

Explanation:

  1. (a > b)5 > 10false → evaluate (b > c ? b : c)
  2. (b > c)10 > 7trueb is chosen

6.5. Key Points

  1. Ternary operator always returns a value.
  2. Can be used in assignments, print statements, or return statements.
  3. For multiple conditions, nest carefully—too much nesting reduces readability.
  4. Works with any data type: int, double, boolean, String, etc.

7️⃣. Bitwise Operators

7.1. Bitwise Operators

Bitwise operators operate on binary representations of integers at the bit level.
They are used to manipulate individual bits of numbers, which is very useful in low-level programming, flags, and performance optimization.

Applicable data types: int, long, short, byte, char.

7.2. Types of Bitwise Operators

A. Bitwise AND (&)
  • Performs AND operation on each pair of corresponding bits.
  • Result is 1 if both bits are 1, else 0.

Example:

int a = 5;   // 0101 in binary
int b = 3;   // 0011 in binary
int c = a & b;
System.out.println(c); // 1 (0001 in binary)

Explanation:

Bit of a0101
Bit of b0011
a & b0001 → 1

B. Bitwise OR (|)
  • Performs OR operation on each pair of bits.
  • Result is 1 if at least one bit is 1, else 0.
int c = a | b;
System.out.println(c); // 7 (0111 in binary)
C. Bitwise XOR (^)
  • Performs XOR operation on each pair of bits.
  • Result is 1 if bits are different, else 0.
int c = a ^ b;
System.out.println(c); // 6 (0110 in binary)
D. Bitwise Complement (~)
  • Inverts all bits (1 → 0, 0 → 1).
  • For signed integers, result = -(n+1) because of two’s complement.
int a = 5;       // 0000 0101
int c = ~a;      // 1111 1010 → -6
System.out.println(c); // -6
E. Left Shift (<<)
  • Shifts all bits to the left by a certain number of positions.
  • Vacant bits are filled with 0.
  • Equivalent to multiplying by 2^n.
int a = 5;       // 0101
int c = a << 2;  // 0101 << 2 → 10100 (20 in decimal)
System.out.println(c); // 20
F. Right Shift (>>)
  • Shifts all bits to the right.
  • For positive numbers, fills leftmost bits with 0.
  • For negative numbers, fills leftmost bits with 1 (sign extension).
  • Equivalent to dividing by 2^n.
int a = 20;      // 10100
int c = a >> 2;  // 10100 >> 2 → 0101 (5)
System.out.println(c); // 5
G. Unsigned Right Shift (>>>)
  • Shifts bits to the right without considering sign.
  • Fills leftmost bits with 0.
  • Only differs from >> for negative numbers.
int a = -20;
int c = a >>> 2;
System.out.println(c); // large positive number
OperatorDescriptionSyntaxExample
&Bitwise ANDa & b5 & 3 → 1
|Bitwise ORa | b`a
^Bitwise XORa ^ b5 ^ 3 → 6
~Bitwise Complement~a~5 → -6
<<Left shifta << 25 << 2 → 20
>>Right shifta >> 25 >> 2 → 1

✅ Example:

int a = 5;  // 0101 in binary
int b = 3;  // 0011 in binary
System.out.println(a & b);  // Output: 1 (0001)
System.out.println(a | b);  // Output: 7 (0111)

🎯Summary

Java provides a comprehensive set of operators that simplify performing calculations, comparisons, logical decisions, and bitwise manipulations.
Understanding their syntax and proper usage is essential for writing clean, efficient, and maintainable Java code.

Java developer with 9+ years of IT experience, sharing tutorials and tips to help learners master Java programming.

Leave a Reply

Your email address will not be published. Required fields are marked *