Java - Thing to know before proceed - Operator Tutorial
An operator is used to perform operations between two operands. Operands can be variables or values and Operator can be +, -, *, / etc.
There are 8 types of an operator in java
-
Arithmetic Operator,
-
Relational Operator,
-
Shift Operator,
-
Logical Operator,
-
Unary Operator,
-
Ternary Operator
-
Bitwise Operator,
-
Assignment Operator.
-
Arithmetic Operator
-
It is used to perform basic mathematics operation i.e addition, subtraction, division, multiplication, and modulus
public class ArithmeticOperator{
public static void main(String args[]){
int x=3;
int y=2;
System.out.println(x+y);//5
System.out.println(x-y);//1
System.out.println(x*y);//6
System.out.println(x/y);//1
System.out.println(x%y);//1
}
}
-
Relational Operator
-
It is used to compare the value of two operands and return the result in the form of the booleans - True or False.
-
Comparison Operator- <, >, <=, >=, instanceof
-
Equality Operator- ==, !=
public class RelationalOperatorExample{ public static void main(String args[]){ int x=7,y=3; System.out.println(x==y); //false System.out.println(x!=y); //True System.out.println(x<=y); //false System.out.println(x>=y); //true System.out.println(x>y); //true System.out.println(x<y); //false }}
-
Shift Operator
-
Left Shift Operator
left shift operator << is used to shift all of the bits in a value to the left side of a specified number of times.
public class LeftShiftOperatorExample{
public static void main(String args[]){
System.out.println(2<<2);//2*2^2=2*4=40
System.out.println(2<<3);//2*2^3=2*8=80
System.out.println(3<<2);//3*2^2=3*4=80
}}
-
Right Shift Operator
Right shift operator >> is used to shift all of the bits in a value to the right side of a specified number of times.
public class RightShiftOperatorExample{
public static void main(String args[]){
System.out.println(20>>2);//20/2^2=20/4=5
System.out.println(20>>3);//20/2^3=20/8=2
System.out.println(30>>2);//30/2^2=30/4=7
}}
-
Logical Operator
a) && Operator
-
In logical && Operator, If anyone condition is false the output will be false.
-
Logical && Operator doesn’t check the second condition if the first condition is false
- Logical && Operator only check the second condition, if the first condition is true.
public class LogicalAndOperatorExample{ public static void main(String args[]){ int x=15; int y=25; int z=35; System.out.println(x<y&&x<z); //true && true =true System.out.println(x<y&&x>z); //true && false =false System.out.println(x>y&&x<z); //false && true =false System.out.println(x>y&&x>z); //false && false =false }}
b) || Operator
-
In logical || Operator, If anyone condition is true the output will be true.
-
Logical || Operator doesn’t check the second condition, if the first condition is true
-
Logical || Operator only check the second condition, if the first condition is false.
public class LogicalOperatorExample{
public static void main(String args[]){
int x=15;
int y=25;
int z=35;
System.out.println(x<y||x<z);
//true && true =true
System.out.println(x<y||x>z);
//true && false =true
System.out.println(x>y||x<z);
//false && true =true
System.out.println(x>y||x>z);
//false && false =false
}}
-
Unary Operator
-
It is used to perform incrementing/decrementing value by one using ++ and --.
public class UnaryOperator{
public static void main(String args[]){
int x=1;
System.out.println(x++);//1
//(p1-Divide x++, into x and ++,
//x will print first 1, then after printing it will get incremented by 1 making it 2)
System.out.println(++x);//3
//(p2-Divdie ++x, into ++ and x,
//++ will increment step-p1 value first making it 3, then x will print the value 3)
System.out.println(x--);//3
//(p3-Divide x--, into x and --, x will print first the previous step-p2 value,
//then after printing it will get decremented by 1 making it 2)
System.out.println(--x);//1
//(p4-Divide --x, into -- and x, -- will first decrement step-p3 value making it 1,
//then after it will print x vlaue i.e 1)
}}
-
Making Value negative using ~
public class UnaryOperator{
public static void main(String args[]){
int x=-2;
int y=2;
System.out.println(~x);//1
//(-2,-1,0,1 So 1 will be output , negating of negative value start from 0)
System.out.println(~y);
//(2,1,0,-1,-2,-3 So 1 will be output , negating of positive value start from -1)
}}
-
inverting the value of a Boolean using!
public class UnaryOperator{
public static void main(String args[]){
boolean x=true;
boolean y=false;
System.out.println(!x);//1
//(This will invert x true value to false)
System.out.println(!y);
//(This will invert y false value to true)
}}
-
Ternary Operator
The ternary operator is used as a one-liner replacement for the if-then-else statement.
public class TernaryOperator{
public static void main(String args[]){
int x=5;
int y=6;
int z=(x<y)?x:y;
System.out.println(z);
}}
-
Bitwise Operator
This operator will check both the condition
public class BitwiseAndOperator{
public static void main(String args[]){
int x=15;
int y=25;
int z=35;
System.out.println(x<y&x<z);
//true && true =true
System.out.println(x<y&x>z);
//true && false =false
System.out.println(x>y&x<z);
//false && true =false
System.out.println(x>y&x>z);
//false && false =false
}}
public class BitwiseOrExample{
public static void main(String args[]){
int x=15;
int y=25;
int z=35;
System.out.println(x<y|x<z);
//true && true =true
System.out.println(x<y|x>z);
//true && false =true
System.out.println(x>y|x<z);
//false && true =true
System.out.println(x>y|x>z);
//false && false =false
}}
-
Assignment Operator
public class AssignmentOperator{
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);
}}