Java Operators

Complete guide to Java operators with examples. Learn arithmetic, assignment, relational, logical, unary, bitwise operators and more.

Operators are symbols that perform operations on variables and values. For example, + is an operator used for addition, while * is also an operator used for multiplication.

Operators in Java can be classified into 6 types:

  1. Arithmetic Operators
  2. Assignment Operators
  3. Relational Operators
  4. Logical Operators
  5. Unary Operators
  6. Bitwise Operators

1. Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations.

OperatorNameDescriptionExample
+AdditionAdds together two valuesx + y
-SubtractionSubtracts one value from anotherx - y
*MultiplicationMultiplies two valuesx * y
/DivisionDivides one value by anotherx / y
%ModulusReturns the division remainderx % y

Example

int x = 10;
int y = 3;
System.out.println(x + y); // 13
System.out.println(x / y); // 3 (Integer division!)
System.out.println(x % y); // 1 (Remainder)

2. Assignment Operators

Assignment operators are used to assign values to variables.

OperatorExampleSame As
=x = 5x = 5
+=x += 3x = x + 3
-=x -= 3x = x - 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
%=x %= 3x = x % 3

3. Relational Operators

Relational operators are used to compare two values. They return a boolean result (true or false).

OperatorNameExample
==Equal tox == y
!=Not equalx != y
>Greater thanx > y
<Less thanx < y
>=Greater than or equal tox >= y
<=Less than or equal tox <= y

4. Logical Operators

Logical operators are used to determine the logic between variables or values.

OperatorNameDescriptionExample
&&Logical ANDReturns true if both statements are truex < 5 && x < 10
||Logical ORReturns true if one of the statements is truex < 5 || x < 4
!Logical NOTReverse the result, returns false if the result is true!(x < 5 && x < 10)

5. Unary Operators

Unary operators are used with only one operand.

OperatorDescriptionExample
++Increment (increases value by 1)++x or x++
--Decrement (decreases value by 1)--x or x--

Prefix vs Postfix

  • Prefix (++x): Increments the variable, then uses the new value.
  • Postfix (x++): Uses the current value, then increments the variable.
int a = 5;
int b = ++a; // a becomes 6, b becomes 6

int c = 5;
int d = c++; // d becomes 5, c becomes 6

6. Bitwise Operators

Bitwise operators work on bits and perform bit-by-bit operations.

OperatorNameDescription
&ANDSets each bit to 1 if both bits are 1
|ORSets each bit to 1 if one of two bits is 1
^XORSets each bit to 1 if only one of two bits is 1
~NOTInverts all the bits
<<Zero fill left shiftShift left by pushing zeros in from the right
>>Signed right shiftShift right by pushing copies of the leftmost bit in from the left

Operator Precedence

Java has well-defined rules for specifying the order in which the operators in an expression are evaluated.

CategoryOperators
Postfixexpr++ expr--
Unary++expr --expr +expr -expr ~ !
Multiplicative* / %
Additive+ -
Shift<< >> >>>
Relational< > <= >= instanceof
Equality== !=
Bitwise AND&
Bitwise XOR^
Bitwise OR|
Logical AND&&
Logical OR||
Ternary? :
Assignment= += -= *= /= %=

Key Takeaways

  • Arithmetic: +, -, *, /, % (remainder).
  • Relational: ==, !=, >, <, >=, <= (return boolean).
  • Logical: && (AND), || (OR), ! (NOT).
  • Assignment: =, +=, -=, etc.

Common Pitfalls

[!WARNING] Equality Check: Use == to compare primitives. NEVER use == to compare Strings (use .equals() instead).

[!WARNING] Precedence: * and / happen before + and -. Use parentheses () to control the order of operations.

Challenge

Challenge

Task:

Calculate the sum of 15 and 25 and print the result.