Operators in Java
In Java, an Operator is a symbol is used to perform operations. Example: -,+,*,/ etc.
Java Operator Precedence
Java Unary Operator
The Java unary operators require only a single operand. Unary operators are utilized to perform different tasks i.e.:
- augmenting/decrementing a value by one
- refuting an expression
- reversing the value of a boolean
Java Unary Operator Example: ++ and --
public static void main(String args[]){
int x=14;
System.out.println(x++);//14 (15)
System.out.println(++x);//15
System.out.println(x--);//15 (14)
System.out.println(--x);//14
}
}
Output:
1415
15
14
Java Unary Operator Example 2: ++ and --
class OperatorExample{
public static void main(String args[]){
int a=14;
int b=14;
System.out.println(a++ + ++a);//14+16=30
System.out.println(b++ + b++);//14+15=29
}
}
Output:
3029
Java Unary Operator Example: ~ and !
class OperatorExample{
public static void main(String args[]){
int a=15;
int b=-15;
boolean c=true;
boolean d=false;
System.out.println(~a);//-16 (minus of total positive
value which starts from 0)
System.out.println(~b);//14 (positive of total minus,
positive starts from 0)
System.out.println(!c);//false (opposite of boolean value)
System.out.println(!d);//true
}
}
Output:
-16
14
false
true
Java Arithmetic Operators
Arithmetic operators in java are used to perform addition, subtraction, multiplication, and division.
Java Arithmetic Operator Example
class OperatorExample{
public static void main(String args[]){
int a=16;
int b=4;
System.out.println(a+b);//20
System.out.println(a-b);//12
System.out.println(a*b);// 64
System.out.println(a/b);//4
System.out.println(a%b);//0
}
}
Output:
20
12
64
4
0
Java Arithmetic Operator Example: Expression\
class OperatorExample{
public static void main(String args[]){
System.out.println(10*10/5+3-1*4/2);
}
}
Output:
21
Java Left Shift Operator
In Java, the left shift operator << is used to shift all of the bits in a value to the left side of a specified number of times.
Java Left Shift Operator Example
class OperatorExample{
public static void main(String args[]){
System.out.println(10<<2);//10*2^2=10*4=40
System.out.println(10<<3);//10*2^3=10*8=80
System.out.println(20<<2);//20*2^2=20*4=80
System.out.println(15<<4);//15*2^4=15*16=240
}
}
Output:
40
80
80
240
Java Right Shift Operator
In Java, the right shift operator >> is used to move the value of the left operand to the right by the number of bits specified by the right operand.
Java Right Shift Operator Example
class OperatorExample{
public static void main(String args[]){
System.out.println(10>>2);//10/2^2=10/4=2
System.out.println(20>>2);//20/2^2=20/4=5
System.out.println(20>>3);//20/2^3=20/8=2
}
}
Output:
2
5
2
Java Shift Operator Example: >> vs >>>
class OperatorExample{
public static void main(String args[]){
//For positive number, >> and >>> works same
System.out.println(20>>2);
System.out.println(20>>>2);
//For negative number, >>> changes parity bit (MSB) to 0
System.out.println(-20>>2);
System.out.println(-20>>>2);
}
}
Output:
5
5
-5
Java AND Operator Example: Logical && and Bitwise &
In Java, The logical && operator doesn't check the second condition if the first condition is false. It checks the second condition only if the first one is true.
The bitwise & operator always checks both conditions whether first condition is true or false.
class OperatorExample{
public static void main(String args[]){
int a=8;
int b=5;
int c=25;
System.out.println(a<b&&a<c);//false && true = false
System.out.println(a<b&a<c);//false & true = false
}
}
Output:
false
false
Java OR Operator Example: Logical || and Bitwise |
The logical || operator doesn't check the second condition if the first condition is true. It checks the second condition only if the first one is false.
The bitwise | operator always checks both conditions whether first condition is true or false.
class OperatorExample{
public static void main(String args[]){
int a=18;
int b=5;
int c=28;
System.out.println(a>b||a<c);//true || true = true
System.out.println(a>b|a<c);//true | true = true
//|| vs |
System.out.println(a>b||a++<c);//true || true = true
System.out.println(a);//8 because second condition is not checked
System.out.println(a>b|a++<c);//true | true = true
System.out.println(a);
}
}
Output:
true
true
true
8
true
9
Java Ternary Operator
Java Ternary operator is used as a one-liner replacement for if-then-else statement and used a lot in Java programming. it is the only conditional operator which takes three operands.
Java Ternary Operator Example
class OperatorExample{
public static void main(String args[]){
int a=2;
int b=5;
int min=(a<b)?a:b;
System.out.println(min);
}
}
Output:
2
Java Assignment Operator
Java assignment operator is one of the most common operators. It is used to assign the value on its right to the operand on its left.
Java Assignment Operator Example
class OperatorExample{
public static void main(String args[]){
int a=10;
int b=20;
a+=4;//a=a+4 (a=10+4)
b-=4;//b=b-4 (b=20-4)
System.out.println(a);
System.out.println(b);
}
}
Output:14
16
Java Assignment Operator Example
class OperatorExample{
public static void main(String[] args){
int a=10;
a+=3;//10+3
System.out.println(a);
a-=4;//13-4
System.out.println(a);
a*=2;//9*2
System.out.println(a);
a/=2;//18/2
System.out.println(a);
}
}
Output:
13
9
18
9
Java Assignment Operator Example: Adding short
class OperatorExample{
public static void main(String args[]){
short a=10;
short b=10;
//a+=b;//a=a+b internally so fine
a=a+b;//Compile time error because 10+10=20 now int
System.out.println(a);
}
}
Output:
Compile time error
After type cast:
class OperatorExample{
public static void main(String args[]){
short a=10;
short b=10;
a=(short)(a+b);//20 which is int now converted to short
System.out.println(a);
}
}
Output:
20
0 Comments