Introduction to Java Operators and types of Java operators

Introduction to Java Operators and types of Java operators

What is Java Operators?

Java Operators are defined as signs or symbols which are used to perform operations. For example: +,=,>,*,/ etc. 

Types of Java Operators

Java programming language provides different types of operators which are on their functionalities. Some of the operators are as follows;

1. Arithmetic Operators

Arithmetic Operators are those operators which are used to perform basic arithmetic operations like addition, division, multiplication, subtraction, etc. 

Arithmetic Operators Example:

public class OperatorExample{  

public static void main(String args[]){  

System.out.println(10*10/5+3-1*4/2);  

}}

Output

21


2. Shift Operators

Shift Operators are those operators which are used to shift all bits of number left or right side of a specified number of times.

Format-

number shift_op number_of_places_to_shift;

Shift Operators are of three types which are as follows;


Left Shift Operators [<<] : It is used to shift all bits of the value to the left side.

Left Shift Operators Example:

public 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


Right Shift Operators [>>] : It is used to shift all bits of the value to the right side.

Right Shift Operators Example:

public 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


3. Unary Operators

Unary Operators are those operators which are used to perform operations like increment, decrement or negate a value.

Operations of Unary Operators are as follows;

Unary minus [-] : It is used to negate the value.

Increment operators [++] : It is used for incrementing value by 1.

Decrement operators [--] : It is used for decrementing value by 1.

Unary Operators Example:

public class OperatorExample{  

public static void main(String args[]){  

int x=10;  

System.out.println(x++);//10 (11)  

System.out.println(++x);//12  

System.out.println(x--);//12 (11)  

System.out.println(--x);//10  

}}

Output

10

12

12

10


4. Ternary Operators

Ternary Operators are those operators which are used as a replacement of if-then-else statement and it includes three operands. 

Format-

condition ? if true : if false

Ternary Operators Example:

public 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


5. Relational Operators

Relational Operators are those operators which are used to check the relations like greater than, equality, less than, etc. 

Format-

variable relation_operator value

6. Logical Operators

Logical Operators are those operators which are used to perform operations like "logical OR" and "logical AND", these are the similar function to OR gate and AND gate in digital electronics.


7. Assignment Operators

Assignment Operators are those operators which are used to assign the value of right operator to the left variable. And there are many cases in which assignment operators are used with other operators.

Format-

variable = value

Assignment Operators Example:

public 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


8. Bitwise Operators

Bitwise Operators are those operators which are used with many integer types during the execution of manipulation of individual bits of values. 

Bitwise Operators are of following types:

• Bitwise AND operator [&]

• Bitwise OR operator [|]

• Bitwise XOR operator [^]

• Bitwise Complement operator [~]

Newest Older

Related Posts

Post a Comment