Loading
Operators are the backbone of programming in Java. They allow you to perform calculations, compare values, manipulate data, and control the flow of your code. Whether you’re adding numbers, checking conditions, or updating variables, you’re using operators. In this guide, we’ll break down every major type of operator in Java, explain how each one works, and provide simple examples to make your learning journey smooth and effective.


1. Arithmetic Operators

Arithmetic operators are used for basic mathematical calculations. You’ll use them whenever you need to add, subtract, multiply, divide, or find the remainder of numbers.


List of Arithmetic Operators 

Operator OperationExampleResult
+Addition10 + 515
-Subtraction10 - 55
*Multiplication10 * 550
/Division10 / 52
%Modulus10 % 50
++Incrementa++ (if a=10)11
--Decrementb-- (if b=5)4


How They Work

  • Addition (+): Adds two numbers together.
  • Subtraction (-): Subtracts the right number from the left.
  • Multiplication (*): Multiplies two numbers.
  • Division (/): Divides the left number by the right. If both numbers are integers, the result is also an integer (fractional part is discarded).
  • Modulus (%): Returns the remainder after division.
  • Increment (++): Increases a variable’s value by 1.
  • Decrement (--): Decreases a variable’s value by 1.


Example:

int a = 10, b = 5;
System.out.println(a + b); // 15
System.out.println(a - b); // 5
System.out.println(a * b); // 50
System.out.println(a / b); // 2
System.out.println(a % b); // 0
a++;
System.out.println(a);     // 11
b--;
System.out.println(b);     // 4


2. Relational Operators

Relational operators help you compare two values. The result is always either true or false. These are essential for making decisions in your code, such as inside if statements.


List of Relational Operators

OperatorMeaningExampleResult
==Equal to a == bfalse
!=Not equal to a != btrue
>Greater thana > btrue
<Less than a < bfalse
>=Greater than or equal toa >= btrue
<=Less than or equal to a <= bfalse


How They Work

  • == checks if two values are exactly the same.
  • != checks if two values are different.
  • > checks if the left value is bigger than the right.
  • < checks if the left value is smaller than the right.
  • >= checks if the left value is bigger than or equal to the right.
  • <= checks if the left value is smaller than or equal to the right.


Example:

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


3. Logical Operators

Logical operators are used to combine or invert boolean values. They’re most often used in conditions where you want to check multiple statements at once.


List of Logical Operators 

OperatorMeaningExampleResult
&&Logical ANDx && yfalse
||Logical OR x
!Logical NOT!xfalse


How They Work

  • && (AND): Returns true only if both sides are true.
  • || (OR): Returns true if at least one side is true.
  • ! (NOT): Flips the value; true becomes false and vice versa.


Example:

boolean x = true, y = false;
System.out.println(x && y); // false (both must be true)
System.out.println(x || y); // true (at least one is true)
System.out.println(!x);     // false (x is true, !x is false)


4. Bitwise Operators

Bitwise operators let you work with the individual bits of integer values. They’re especially useful in low-level programming, performance optimization, and graphics.


List of Bitwise Operators

OperatorMeaningExampleResult
&Bitwise ANDa & b1
|Bitwise OR a | b7
^Bitwise XORa ^ b6
~Bitwise NOT~a-6
<<Left Shifta << 110
>>Right Shifta >> 12
>>>Unsigned Right Shifta >>> 12


How They Work

  • & (AND): Each bit in the result is 1 only if both bits are 1.
  • | (OR): Each bit in the result is 1 if at least one bit is 1.
  • ^ (XOR): Each bit in the result is 1 if only one bit is 1.
  • ~ (NOT): Flips every bit (1 to 0, 0 to 1).
  • << (Left Shift): Shifts bits to the left (multiplies by 2 for each shift).
  • >> (Right Shift): Shifts bits to the right (divides by 2 for each shift).
  • >>> (Unsigned Right Shift): Like right shift, but fills leftmost bits with 0.


Example:

int a = 5; // 0101 in binary
int b = 3; // 0011 in binary
System.out.println(a & b);  // 1  (0001)
System.out.println(a | b);  // 7  (0111)
System.out.println(a ^ b);  // 6  (0110)
System.out.println(~a);     // -6 (in binary, flips all bits)
System.out.println(a << 1); // 10 (1010)
System.out.println(a >> 1); // 2  (0010)
System.out.println(a >>> 1);// 2  (0010)


5. Assignment Operators

Assignment operators are used to assign values to variables. Some also combine assignment with arithmetic operations, making your code shorter and easier to read.


List of Assignment Operators

OperatorMeaningExampleResult
=Assigna = 1010
+=Add and assigna += 515
-=Subtract and assigna -= 312
*=Multiply and assigna *= 224
/=Divide and assigna /= 46
%=Modulus and assigna %= 30


How They Work

  • = assigns a value to a variable.
  • += adds a value to the variable and assigns the result.
  • -= subtracts a value from the variable and assigns the result.
  • *= multiplies the variable by a value and assigns the result.
  • /= divides the variable by a value and assigns the result.
  • %= finds the remainder and assigns the result.


Example:

int a = 10;
a += 5; // a = a + 5; a is now 15
a -= 3; // a = a - 3; a is now 12
a *= 2; // a = a * 2; a is now 24
a /= 4; // a = a / 4; a is now 6
a %= 3; // a = a % 3; a is now 0
System.out.println(a); // 0


6. Conditional ( Ternary ) Operator

The conditional or ternary operator is a shortcut for simple if-else statements. It evaluates a condition and returns one value if true, another if false.


Syntax:

result = (condition) ? valueIfTrue : valueIfFalse;


Example:

int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println(max); // 20

If a > b is true, max gets the value of  a; otherwise, it gets the value of b.


7. Unary Operators

Unary operators work with only one operand. They are used for tasks like incrementing, decrementing, negating, or inverting values.


List of Unary Operators 

OperatorMeaningExampleResult
+Unary plus+a5
-Unary minus-a-5
++Incrementa++6
--Decrementa--5
!Logical NOT!bfalse


How They Work

  • + indicates a positive value (rarely used, as numbers are positive by default).
  • - negates a value (makes positive numbers negative, and vice versa).
  • ++ increases a variable’s value by 1.
  • -- decreases a variable’s value by 1.
  • ! inverts a boolean value.


Example:

int a = 5;
System.out.println(+a); // 5
System.out.println(-a); // -5
a++;
System.out.println(a);  // 6
a--;
System.out.println(a);  // 5
boolean b = true;
System.out.println(!b); // false


Operators are essential tools in Java programming. They allow you to perform calculations, compare values, make decisions, and manipulate data efficiently. By mastering operators, you’ll write cleaner, more powerful, and more flexible code.


Tips: Experiment with each operator type in your own Java programs. Try combining them in different ways to see how they interact and affect your results.