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:
- Addition (+): Adds two operands together.
- Subtraction (-): Subtracts the second operand from the first operand.
- Multiplication (*): Multiplies two operands together.
- Division (/): Divides the first operand by the second operand.
- 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:
- Equal to (==) – This operator checks if two values are equal to each other.
- Not equal to (!=) – This operator checks if two values are not equal to each other.
- Greater than (>) – This operator checks if the first value is greater than the second value.
- Less than (<) – This operator checks if the first value is less than the second value.
- Greater than or equal to (>=) – This operator checks if the first value is greater than or equal to the second value.
- 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:
- 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.
- 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.
- 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.
- Bitwise complement (~) – This operator flips all the bits of its operand, converting 0s to 1s and vice versa.
- 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.
- 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:
- Addition assignment (+=) – adds the value on the right-hand side to the variable on the left-hand side.
- Subtraction assignment (-=) – subtracts the value on the right-hand side from the variable on the left-hand side.
- Multiplication assignment (*=) – multiplies the variable on the left-hand side by the value on the right-hand side.
- Division assignment (/=) – divides the variable on the left-hand side by the value on the right-hand side.
- Modulus assignment (%=) – calculates the remainder when dividing the variable on the left-hand side by the value on the right-hand side.
- 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.
- 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.
- 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.
- 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.
- 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
- 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.
- Instanceof operator – This operator is used to check whether an object is an instance of a particular class or interface.
- Dot operator (.) – This operator is used to access the members of a class, such as fields and methods.
- 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:
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:
Precedence | Operator | Type | Associativity |
---|---|---|---|
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 AND | Left to right |
6 | ^ | Bitwise exclusive OR | Left to right |
5 | | | Bitwise inclusive OR | Left to right |
4 | && | Logical AND | Left to right |
3 | || | Logical OR | Left to right |
2 | ? : | Ternary conditional | Right 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
Login/Signup to comment