Java Operators

Home / Core Java / Java Operators

Java Operators


Java Operators :

  In Java, operators are symbols that are used to perform operations on variables and values.
  There are several types of operators in Java, categorized based on their functionality.

  Here are the main types of operators:
  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Bitwise operators


Arithmetic Operators

Arithmetic operators are used to perform mathematical operations on numerical values.

+ (Addition):     Adds two operands.

  • Example: int sum = 5 + 3; // Here, sum will be 8

- (Subtraction): Subtracts the second operand from the first.

  • Example: int difference = 7 - 4; // Here, difference will be 3

* (Multiplication): Multiplies two operands.

  • Example: int product = 2 * 6; // Here, product will be 12

/ (Division): Divides the first operand by the second.

  • Example: int quotient = 10 / 3; // Here, quotient will be 3

% (Modulus): Returns the remainder of the division.

  • Example: int remainder = 10 % 3; // Here, remainder will be 1


Assignment Operators

Assignment operators are used to assign values to variables.

= (Assignment): Assigns the value on the right to the variable on the left.

  • Example: int x = 10;

Compound Assignment Operators (e.g., +=, -=, *=, /=, %=): Perform an operation on the variable and then assign the result back to the variable.

  • Example: x += 5; // Same as x = x + 5;


Comparison Operators

Comparison operators are used to compare two values:

== (Equal to): Compares if two values are equal.

!= (Not equal to): Compares if two values are not equal.

> (Greater than): Checks if the value on the left is greater than the value on the right.

< (Less than): Checks if the value on the left is less than the value on the right.

>= (Greater than or equal to): Checks if the value on the left is greater than or equal to the value on the right.

<= (Less than or equal to): Checks if the value on the left is less than or equal to the value on the right.


Logical Operators

Logical operators are used for combining conditional statements.

&& (Logical AND): Returns true only if both conditions are true.

  • Example: boolean result = (true && false); // Here, result will be false

|| (Logical OR): Returns true if at least one condition is true.

  • Example: boolean result = (true || false); // Here, result will be true

! (Logical NOT): Reverses the logical state of its operand.

  • Example: boolean result = !(true); // Here, result will be false



Bitwise Operators

Bitwise operators perform operations at the bit level.

& (Bitwise AND), | (Bitwise OR), ^ (Bitwise XOR): Perform logical operations on individual bits of the operands.

~ (Bitwise Complement): Flips the bits of a value.

<< (Left Shift), >> (Right Shift), >>> (Unsigned Right Shift): Shift the bits of a value left or right.


Java Operators Precedence 

         

Operator Category 
 Operators
 Precedence
Postfix
expr++, expr--
1
Unary
++expr, --expr, +expr, -expr, ~, !
2
Multiplicative
*, /, %
3
Additive
+, -
4
Shift
<<, >>, >>>
5
Relational
<, >, <=, >= 
6
Equality
==, !=
7
Bitwise AND
&8
Bitwise XOR
^9
Bitwise OR
|10
Logical AND
&&11
Logical OR
||12
Conditional (Ternary)
? :13
Assignment
=, +=, -=, /=, %= and others
14

    

Arithmetic Operators Example :



Comparison Operators Example :


Assignment Operators Example :


Logical Operators Example :


Bitwise Operators Example :