Operators in Java
Operators are special symbols that are used to perform a specific operation on operands (variables and values) and then return a result. For example, + is an arithmetic operator which is used to perform an addition between two operands. An operand refers to a variable, object or literal.
Example – a = b + c - d;
Above expression contains four operands (a, b, c, d) and three operators (+, -, =).
Types of Operators in Java
- Unary Operator
- Arithmetic Operator
- Shift Operator
- Relational Operator
- Bitwise Operator
- Logical Operator
- Ternary Operator
- Assignment Operator
Below table contains the list of operators according to their precedence order.
Precedence | Operator | Type | Associativity |
15 | () [] · |
Parentheses Array subscript Member selection |
Left to Right |
14 | ++ -- |
Unary post-increment Unary post-decrement |
Right to left |
13 | ++ -- + - ! ~ ( type ) |
Unary pre-increment Unary pre-decrement Unary plus Unary minus Unary logical negation Unary bitwise complement Unary typecast |
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 |
< |
Relational less 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 |
A larger number means higher precedence. It means high precedence operator in an expression is always evaluated before the low precedence operator.
int a = 2 + 4 * 8;
The result of the above expression is 34 because * has higher precedence than + so 4*8 evaluated first then 2 will be added I result.
If multiple operators n an expression have the same precedence then they are always evaluated from left to right.
Assignment, Unary and Ternary operators have the exception they are evaluated from right to left. If one expression contains multiple = operators they are evaluated from right to left instead of left to right.
int a = b = c = d = 5;
Above expression evaluates d=5 first then c=d and then b=c and then a = b.
Java Unary Operators
The Unary operators require only one operand. They perform various operations like incrementing/ decrementing the value by one, negative an expression or inverting the value of a Boolean.
Operator | Description |
+ | Unary plus operator; indicates the positive value (numbers are positive without this, however) |
- | Unary minus operator; negates an expression |
++ | Increment operator; increments a value by 1 |
-- | Decrement operator; decrements a value by 1 |
! | Logical complement operator; inverts the value of a boolean |
The increment/ decrement operator can be used before or after any operand. Both statement ++I or i++ increment value of I by 1.
The increment/decrement operators can be applied before (prefix) or after (postfix) the operand. The code result++ and ++result statement increment result value by 1. The only difference is that the prefix version (++result) evaluates to the incremented value, whereas the postfix version (result++) evaluates to the original value. If you are just performing a simple increment/decrement, it doesn't really matter which version you choose. But if you use this operator in part of a larger expression, the one that you choose may make a significant difference.
To understand better, have a look below example
Java Unary Operators Example:
class mydemo{
public static void main(String args[]){
int z=10;
System.out.println(z++);
System.out.println(++z);
System.out.println(z--);
System.out.println(--z);
}
}
Java Unary Operator Example: ~ and!
class mydemo{
public static void main(String args[]){
int x=10;
int y=-10;
boolean z=true;
boolean a=false;
System.out.println(~x);
System.out.println(~y);
System.out.println(!z);
System.out.println(!a);
}
}
Java Arithmetic Operators
Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. Binary operators (+, -, *, /, %) are arithmetic operators.
The only new operator here is “%” which divides one operand by another and returns the remainder as to its result. Here is an example of expression that returns result 2.
int remainder = 17 % 5; // reminder is 2
Unary operators ++ (increment) and – (decrement) are used for arithmetic calculations
int I = 5;
i++; // value will be 6
Compound operators +=, -=, *=, /= and %- can also in arithmetic calculation.
int I = 5;
I *= 10 // i=50;
Java Arithmetic Operators Example
class mydemo{
public static void main(String args[]){
int x=10;
int y=5;
System.out.println(x+y);
System.out.println(x-y);
System.out.println(x*y);
System.out.println(x/y);
System.out.println(x%y);
}
}
Java Relational Operators
The relational operators determine if one operand is greater than, less than, equal to or not equal to another operand.
Assume variable x holds 10 and variable y holds 20 then:
== | Equal to | (x == y) is not true |
!= | Not Equal to | (x != y) is true |
> | Greater Than | (x > y) is not true |
>= | Greater than or Equal to | (x >= y) is not true |
< | Less Than | (x < y) is true. |
<= | Less Than or Equal to | (x <= y) is true. |
To understand in details, have a look below program
public class mydemo {
public static void main(String args[]) {
int x = 10;
int y = 20;
System.out.println("x == y = " + (x == y) );
System.out.println("x != y = " + (x != y) );
System.out.println("x > y = " + (x > y) );
System.out.println("x < y = " + (x < y) );
System.out.println("y >= x = " + (y >= x) );
System.out.println("y <= x = " + (y <= x) );
}
}
Java Conditional Operators
The && operator and || operator perform conditional AND and conditional OR operators on two Boolean expressions.
To understand it, have a look below example-
public class mydemo
{
public static void main(String[] args) {
int value1 = 1;
int value2 = 2;
if ((value1 == 1) && (value2 == 2))
System.out.println("value1 is 1 and value2 is 2");
if ((value1 == 1) || (value2 == 1))
System.out.println("value1 is 1 OR value2 is 1");
}
}
Another example of a conditional operator is? : operator. This operator can be used in case of if then else statement. Actually, it is one-liner if else statement.
public class mydemo2{
public static void main(String[] args) {
int value1 = 1;
int value2 = 2;
int result;
boolean someCondition = true;
result = someCondition ? value1 : value2;
System.out.println(result);
}
}
This operator also is known as a Java Ternary operator.
Java Left Shift Operators Example
The Java left shift operator is used to shift all of the bits in fee to the left aspect of a particular quantity of instances.
class mydemo{
public static void main(String args[]){
System.out.println(20<<2);//20*2^2=20*4=80
System.out.println(30<<3);//30*2^3=30*8= 240
System.out.println(40<<2);//40*2^2=40*4= 160
System.out.println(50<<4);//50*2^4=50*16= 800
}}
Java Right Shift Operators
The Java right shift operator is used to move left operands cost to right via the variety of bits detailed by using the right operand.
class mydemo{
public static void main(String args[]){
System.out.println(40>>2);//40/2^2=40/4=10
System.out.println(20>>2);//20/2^2=20/4=5
System.out.println(5>>3);//5/2^3=5/8=0
}}
Java Bitwise Operators
Bitwise operators work on bits and perform bit by bit operation.
Assume if a = 60; and b = 13; Now in the binary format they will be as follows:
A | B | A|B | A&B | A^B | ~A |
0 | 0 | 0 | 0 | 0 | 1 |
1 | 0 | 1 | 0 | 1 | 0 |
0 | 1 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 | 0 |
Java Bitwise Operators Example
public class demo {
public static void main(String args[]) {
int x = 70;
int y = 25;
int z = 0;
z = x & y;
System.out.println("x & y = " + z );
z = x | y;
System.out.println("x | y = " + z );
z= x ^ y;
System.out.println("x ^ y = " + z );
z = ~x;
System.out.println("~x = " + z );
z = x << 2;
System.out.println("x << 2 = " + z );
z = x >> 2;
System.out.println("x >> 2 = " + z );
z = x >>> 2;
System.out.println("x >>> 2 = " + z );
}
}
Java Logical Operators
Assume boolean variables X holds true and variable Y holds false then:
&& | (X && Y) is false. |
|| | (X || Y) is true. |
! | !(X && Y) is true |
Logical Operators Example
public class mydemo {
public static void main(String args[]) {
boolean x = true;
boolean y = false;
System.out.println("x && y = " + (x&&y));
System.out.println("x || y = " + (x||y) );
System.out.println("!(x && y) = " + !(x && y));
}
}
Java Assignment Operators
Assignment operators are used to assigning values to variables.
Operator | Name | Example |
= | Assignment | z = x + y will assign value of x + y into z |
+= | Addition assignment | z += x is equivalent to z = z + x |
-= | Subtraction assignment | z -= x is equivalent to z = z - x |
*= | Multiplication assignment | z *= x is equivalent to z = z * x |
/= | Division assignment | z /= x is equivalent to z = z / x |
%= | Modulus assignment | z %= x is equivalent to z = z % x |
<<= | Left shift AND assignment operator | z <<= 2 is same as z = z << 2 |
>>= | Right shift AND assignment operator | z >>= 2 is same as z = z >> 2 |
&= | Bitwise AND assignment operator | z &= 2 is same as z = z & 2 |
^= | Bitwise exclusive OR and assignment operator | z ^= 2 is same as z = z ^ 2 |
|= | Bitwise inclusive OR and assignment operator | z |= 2 is same as z = z | 2 |
Java Assignment Operators Example
public class mydemo {
public static void main(String args[]) {
int x = 40;
int y = 50;
int z = 0;
z = x + y;
System.out.println("z = x + y = " + z );
z += x ;
System.out.println("z += x = " + z );
z -= x ;
System.out.println("z -= x= " + z );
z *= x ;
System.out.println("z *= x = " + z );
x = 30;
z = 45;
z /= z ;
System.out.println("z /= x = " + z );
x = 30;
z = 45;
z %= x ;
System.out.println("z %= x = " + z );
z <<= 2 ;
System.out.println("z <<= 2 = " + z );
z >>= 2 ;
System.out.println("z >>= 2 = " + z );
z >>= 2 ;
System.out.println("z >>= 2 = " + z );
z &= x ;
System.out.println("z &= x = " + z );
z ^= x ;
System.out.println("z ^= x = " + z );
z |= x ;
System.out.println("z |= x = " + z );
}
}