Operators in java
In this blog we will discuss what is Operators in Java?What is the types of Operators in Java?
Operators in java:
Operators is nothing, it is just a symbol that is used to perform operations. Like +,-,* etc.
Types Of Operators:
Operators
Symbol
Unary Operator
exp++,exp — , — exp,++exp
Arithmetic Operator
* / % + -
Shift Operator
<< >> >>>
Relational Operator
< > <= >= instanceof,!=
Bitwise Operator
&,|,^
Logical Operator
&&,||,
Ternary Operator
? :
Assignment Operator
=,+=,-=,*=
Java Unary Operator:
Java Unary need only one operand.It is used to increment or decrement value of Operand by 1.
class UnaryOperatorExample{
public static void main(String args[]){
int x=11;
System.out.println(x++);//11 (12)
System.out.println(++x);//13
System.out.println(x — );//13 (12)
System.out.println( — x);//11
}}
Java Arithmetic Operators:
Java Arithmetic operatores are used to perform Maths operations like addition,Sub,Multiply,Modules and Divison.
class ArithmeticsOperatorExample{
public static void main(String args[]){
int a=10;
int b=2;
System.out.println(a/b);//5
System.out.println(a%b);//0
System.out.println(a+b);//12
System.out.println(a-b);//8
System.out.println(a*b);//20
}}
Java Shift Operator:
Java Left Shift Operator: The Java left shift operator << is used to shift all of the bits in a value to the left side of a specified number of times.
class LeftShiftOperatorExample{
public static void main(String args[]){
System.out.println(10<<2);//10*2²=10*4=40
System.out.println(10<❤);//10*2³=10*8=80
System.out.println(20<<2);//20*2²=20*4=80
System.out.println(15<<4);//15*2⁴=15*16=240
}}
Java Right Shift Operator: The Java right shift operator >> is used to move left operands value to right by the number of bits specified by the right operand.
class RightShiftOperatorExample{
public static void main(String args[]){
System.out.println(10>>2);//10/2²=10/4=2
System.out.println(20>>2);//20/2²=20/4=5
System.out.println(20>>3);//20/2³=20/8=2
}}
Java AND Operator Example: Logical && and Bitwise &:
Main differnce between logical && and Bitwise & is Bitwise & will check all conditions but logical && will check 2nd condition if 1st condition is true otherwise it will not be checked.
class AndOperatorExample{
public static void main(String args[]){
int x=10;
int y=5;
int z=20;
System.out.println(x<y&&x<z);//false && true = false
System.out.println(x<y&x<z);//false & true = false
}}
Java OR Operator Example: Logical || and Bitwise |:
Main difference between logical || and Bitwise | is that Bitwise will check all the condition if 1st condition is true or false but logical || will check 2nd condition if 1st is true otherwise it will not be checked.
class OrOperatorExample{
public static void main(String args[]){
int x=15;
int y=10;
int z=30;
System.out.println(x>y||x<z);//true || true = true
System.out.println(x>y|x<z);//true | true = true
}
}
Java Ternary Operator:
Java Ternary operator is one line replacement of If-Else. It contains 3 operands. It is very important for java as it is used a lot in java.
class TernaryOperatorExample{
public static void main(String args[]){
int x=5;
int y=8;
int min=(a<b)?a:b; //5
System.out.println(min);
}}
Syntax: condition?(returned if condition is true):(returned if condition is false);
Java Assignment Operator:
Assignment operator is most commonly used operator. It is used to assign value to variable.
int a=10;
value 10 is assigned to variable a that is type of Integer.
Please comment,follow and like this story.