Operators in Java

In Java, operators are special symbols that perform operations on variables and values.
They can be grouped into several categories:


1. Arithmetic Operators

Used for basic mathematical operations.

OperatorDescriptionExample (a=10, b=3)Result
+Additiona + b13
-Subtractiona - b7
*Multiplicationa * b30
/Divisiona / b3
%Modulus (remainder)a % b1
++Increment (by 1)a++11
--Decrement (by 1)a--9

2. Relational Operators

Used to compare two values (result is true or false).

OperatorMeaningExample (a=10, b=3)Result
==Equal toa == bfalse
!=Not equal toa != btrue
>Greater thana > btrue
<Less thana < bfalse
>=Greater than or equal toa >= btrue
<=Less than or equal toa <= bfalse

3. Logical Operators

Used to combine conditions.

OperatorMeaningExampleResult
&&Logical AND(a > b) && (a < 20)true
||Logical OR(a < b) || (a < 20)true
!Logical NOT!(a > b)false

4. Assignment Operators

Used to assign values.

OperatorExampleEquivalent to
=a = 5a = 5
+=a += 3a = a + 3
-=a -= 3a = a – 3
*=a *= 3a = a * 3
/=a /= 3a = a / 3
%=a %= 3a = a % 3

5. Unary Operators

Operate on a single operand.

OperatorDescription
+Positive (no change)
-Negation
++Increment
--Decrement
!Logical NOT

6. Bitwise Operators

Operate on bits.

OperatorMeaningExample (a=5, b=3)Binary Result
&ANDa & b1 (0001)
|ORa | b7 (0111)
^XORa ^ b6 (0110)
~Bitwise NOT~a-6
<<Left shifta << 110 (1010)
>>Right shifta >> 12 (0010)
>>>Unsigned right shifta >>> 12

7. Ternary Operator

A shorthand for if-else.

int result = (a > b) ? a : b; // If a>b, result=a else result=b

8. Instanceof Operator

Checks if an object is an instance of a specific class.

if (obj instanceof String) { ... }

If you want, I can make you a Java table with all operators, precedence, and associativity so you know the exact order they get evaluated. That’s very useful for coding interviews and tricky expressions. Would you like me to prepare that?

In Java, both i++ and ++i increase the value of i by 1, but the difference is when the increment happens in relation to the expression’s evaluation.


1. i++ → Post-increment

  • Increment happens after the current value is used in the expression.
  • Steps:
    1. Take the current value of i and use it.
    2. Then increment i by 1.

Example:

int i = 5;
int x = i++; // x = 5, i = 6

Here:

  • x gets the old value (5).
  • i is incremented afterwards (now 6).

2. ++i → Pre-increment

  • Increment happens before the value is used in the expression.
  • Steps:
    1. Increment i by 1.
    2. Use the new value.

Example:

int i = 5;
int x = ++i; // x = 6, i = 6

Here:

  • i increments first (becomes 6).
  • x gets the new value (6).

3. Side-by-side comparison

ExpressionIncrement timingValue returnedFinal i value
i++After value is usedOld valueOld value + 1
++iBefore value is usedNew valueOld value + 1

4. Example inside a loop

for (int i = 0; i < 3; i++) {
    System.out.println(i); // prints 0, 1, 2
}

Here i++ increments after each loop iteration’s body runs.
If you used ++i here, the loop’s behavior would be the same because the increment happens at the end anyway — the difference only matters inside expressions where the increment and usage happen together.


Leave a Reply