Java Operators

Java Operators

What are Operators?

In the Article, we will Discuss about the Operators of java.
In Java, operators are special symbols or keywords that perform operations on one or more operands. operators are symbols or keywords that perform specific operations on one or more operands (values or variables). 

Java Operators Precedence:

Java operator precedence determines the order in which operators are evaluated in an expression. Operators with higher precedence are evaluated before operators with lower precedence. If two operators have the same precedence, then their associativity determines the order of evaluation.

Types Of Operators in Java:

In Java, there are Six types of Operators:

The basic arithmetic operators are:

  1. Addition (+): Adds two operands together.
  2. Subtraction (-): Subtracts the second operand from the first operand.
  3. Multiplication (*): Multiplies two operands together.
  4. Division (/): Divides the first operand by the second operand.
  5. Modulus (%): Returns the remainder of the division operation.

Example:

int a = 10;
int b = 20;
int c = a + b; // Addition
int d = b - a; // Subtraction
int e = a * b; // Multiplication
int f = b / a; // Division
int g = b % a; // Modulus

There are six relational operators in Java:

  1. Equal to (==) – This operator checks if two values are equal to each other.
  2. Not equal to (!=) – This operator checks if two values are not equal to each other.
  3. Greater than (>) – This operator checks if the first value is greater than the second value.
  4. Less than (<) – This operator checks if the first value is less than the second value.
  5. Greater than or equal to (>=) – This operator checks if the first value is greater than or equal to the second value.
  6. Less than or equal to (<=) – This operator checks if the first value is less than or equal to the second value.

Example:

int a = 10;
int b = 5;
boolean result = a > b;
System.out.println(result);

There are six bitwise operators in Java:

  1. Bitwise AND (&) – This operator performs a logical AND operation on each pair of corresponding bits in the operands, returning 1 for each bit position where both bits are 1.
  2. Bitwise OR (|) – This operator performs a logical OR operation on each pair of corresponding bits in the operands, returning 1 for each bit position where at least one of the bits is 1.
  3. Bitwise XOR (^) – This operator performs a logical XOR (exclusive OR) operation on each pair of corresponding bits in the operands, returning 1 for each bit position where exactly one of the bits is 1.
  4. Bitwise complement (~) – This operator flips all the bits of its operand, converting 0s to 1s and vice versa.
  5. Left shift (<<) – This operator shifts the bits of its left-hand operand to the left by a specified number of positions, filling the vacated positions with 0s.
  6. Right shift (>>) – This operator shifts the bits of its left-hand operand to the right by a specified number of positions, filling the vacated positions with copies of the leftmost bit (which is the sign bit for signed types).

Example:

int a = 5; // binary representation: 00000101
int b = 3; // binary representation: 00000011

int c = a & b; // result: 00000001 (binary) = 1 (decimal)
int d = a | b; // result: 00000111 (binary) = 7 (decimal)
int e = a ^ b; // result: 00000110 (binary) = 6 (decimal)
int f = ~a; // result: 11111010 (binary) = -6 (decimal)
int g = a << 2; // result: 00010100 (binary) = 20 (decimal)
int h = a >> 1; // result: 00000010 (binary) = 2 (decimal)

Example:

int a = 10;
int b = 5;
boolean result = a > b;
System.out.println(result);

The compound assignment operators in Java are:

  1. Addition assignment (+=) – adds the value on the right-hand side to the variable on the left-hand side.
  2. Subtraction assignment (-=) – subtracts the value on the right-hand side from the variable on the left-hand side.
  3. Multiplication assignment (*=) – multiplies the variable on the left-hand side by the value on the right-hand side.
  4. Division assignment (/=) – divides the variable on the left-hand side by the value on the right-hand side.
  5. Modulus assignment (%=) – calculates the remainder when dividing the variable on the left-hand side by the value on the right-hand side.
  6. Bitwise AND assignment (&=) – performs a bitwise AND operation on the variable on the left-hand side and the value on the right-hand side, and assigns the result back to the variable.
  7. Bitwise OR assignment (|=) – performs a bitwise OR operation on the variable on the left-hand side and the value on the right-hand side, and assigns the result back to the variable.
  8. Bitwise XOR assignment (^=) – performs a bitwise XOR operation on the variable on the left-hand side and the value on the right-hand side, and assigns the result back to the variable.
  9. Left shift assignment (<<=) – shifts the bits of the variable on the left-hand side to the left by the number of positions specified by the value on the right-hand side, and assigns the result back to the variable.
  10. Right shift assignment (>>=) – shifts the bits of the variable on the left-hand side to the right by the number of positions specified by the value on the right-hand side, and assigns the result back to the variable.

Example:

int x = 10;
x += 5; // x is now 15
x -= 3; // x is now 12
x *= 2; // x is now 24
x /= 4; // x is now 6
x %= 2; // x is now 0
x &= 3; // x is now 0
x |= 5; // x is now 5
x ^= 3; // x is now 6
x <<= 2; // x is now 24
x >>= 1; // x is now 12
  1. Ternary operator (? 🙂 – This operator is also called the conditional operator. It is used to assign one of two values to a variable depending on a condition.
  2. Instanceof operator – This operator is used to check whether an object is an instance of a particular class or interface.
  3. Dot operator (.) – This operator is used to access the members of a class, such as fields and methods.
  4. Comma operator (,) – This operator is used to separate expressions in a statement. The expressions are evaluated from left to right, and the value of the last expression is returned.

Java Example Program For Operator Precedence:

Run
import java.util.*;

public class Main{
    public static void main(String[] args){
        // Initializing the variables
        int a = 10;
        int b = 20;
        int c = 30;
        int d = 60;
        int e;

        //10 + 600
        e = a + b * c;          
        System.out.println(e);

        //30 * 30
        e = (a + b) * c;     
        System.out.println(e);

        //30 * 2        
        e = (a + b) * (d / c);  
        System.out.println(e);
    }
}

Output:

610
900
60

Explanation:

In the above Example of Operator precedence, We had taken different Integer type Variables and we are testing the precedence of different operators with the help of different Examples throughout the program.

Java Precedence And Associativity Table:

PrecedenceOperatorTypeAssociativity
15()
[]
·
Parentheses
Array subscript
Member selection

Left to Right

14++
Unary post-increment
Unary post-decrement
Left to Right
13++

+

!
~
( type )
Unary pre-increment
Unary pre-decrement
Unary plus
Unary minus
Unary logical negation
Unary bitwise complement
Unary type cast
Right to left
12*
/
%
Multiplication
Division
Modulus
Left to right
11+
Addition
Subtraction
Left to right
10<<
>>
>>>
Bitwise left shift
Bitwise right shift with sign extension
Bitwise right shift with zero extension
Left to right
9<
<=
>
>=
instanceof
Relational less than
Relational less than or equal
Relational greater than
Relational greater than or equal
Type comparison (objects only)
Left to right
8==
!=
Relational is equal to
Relational is not equal to
Left to right
7&Bitwise ANDLeft to right
6^Bitwise exclusive ORLeft to right
5|Bitwise inclusive ORLeft to right
4&&

Logical AND

Left to right
3||Logical ORLeft to right
2? :Ternary conditionalRight to left
1=
+=
-=
*=
/=
%=
Assignment
Addition assignment
Subtraction assignment
Multiplication assignment
Division assignment
Modulus assignment
Right to left

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription