Skip to main content

Operators in Java

Hey there! In this guide, we'll explore operators in Java. Operators are symbols that instruct the compiler to perform specific operations on variables or values. Java supports a variety of operators, including arithmetic, relational, logical, bitwise, and more. Let's dive in!

  • Operators are symbols that instruct the compiler to perform specific operations on variables or values.
  • Java supports a variety of operators, including arithmetic, relational, logical, bitwise, and more.

1. Arithmetic Operators​

Arithmetic operators perform mathematical operations such as addition, subtraction, multiplication, and division.

OperatorDescriptionExample
+Additionx + y
-Subtractionx - y
*Multiplicationx * y
/Divisionx / y
%Modulus (remainder)x % y

Example:​

int x = 10, y = 5;
System.out.println(x + y); // Output: 15
System.out.println(x - y); // Output: 5
System.out.println(x * y); // Output: 50
System.out.println(x / y); // Output: 2
System.out.println(x % y); // Output: 0

2. Relational Operators​

Relational operators compare two values and return a boolean result (true or false).

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

Example:​

int x = 10, y = 5; System.out.println(x == y);  // Output: false
System.out.println(x != y); // Output: true
System.out.println(x > y); // Output: true
System.out.println(x < y); // Output: false
System.out.println(x >= y); // Output: true
System.out.println(x <= y); // Output: false

3. Logical Operators​

Logical operators are used to perform logical operations and combine multiple conditions.

OperatorDescriptionExample
&&Logical AND(x > 5 && y < 10)
||Logical OR(x > 5 || y < 10)
!Logical NOT!(x > 5)

Example:​

int x = 10, y = 5; System.out.println(x > 5 && y < 10);  // Output: true
System.out.println(x > 5 || y > 10); // Output: true
System.out.println(!(x > 5)); // Output: false

4. Assignment Operators​

Assignment operators are used to assign values to variables.

OperatorDescriptionExample
=Assigns valuex = y
+=Adds and assignsx += y
-=Subtracts and assignsx -= y
*=Multiplies and assignsx *= y
/=Divides and assignsx /= y
%=Modulus and assignsx %= y

Example:​

int x = 10, y = 5; x += y;  // Equivalent to x = x + y
System.out.println(x); // Output: 15

5. Increment and Decrement Operators​

Increment and decrement operators are used to increase or decrease a variable's value by 1.

OperatorDescriptionExample
++Increments value++x or x++
--Decrements value--x or x--

Example:​

int x = 10; System.out.println(++x);  // Output: 11 (Pre-increment)
System.out.println(x--); // Output: 11 (Post-decrement)
System.out.println(x); // Output: 10

6. Bitwise Operators​

Bitwise operators operate on bits and perform bit-level operations.

OperatorDescriptionExample
&Bitwise ANDx & y
|Bitwise OR|
^Bitwise XORx ^ y
~Bitwise NOT~x
>>Right shiftx >> 2
<<Left shiftx << 2

Example:​

int x = 10, y = 5; System.out.println(x & y);   // Output: 0
System.out.println(x | y); // Output: 15

7. Ternary Operator​

The ternary operator is a shorthand for an if-else statement.

OperatorDescriptionExample
?:Ternarycondition ? expr1 : expr2

Example:​

int x = 10; int result = (x > 5) ? 100 : 200;
System.out.println(result); // Output: 100