Operators
An operator, in Java, is a special symbol performing specific operations on one, two, or three operands and then returning a result. They are classified and listed according to precedence order. Java operators are generally used to manipulate primitive data types. The Java operators are classified into eight different categories: assignment, arithmetic, relational, logical, bitwise, compound assignment, conditional, and type comparison operators.
Fig: Operators in Java
a. Unary operators
The Java unary operators require only one operand. Unary operators are used to performing various operations i.e.:
- incrementing/decrementing a value by one negating an expression,
- inverting the value of a boolean
b. Arithmetic Operators
Java arithmetic operators are used to perform addition, subtraction, multiplication, and division.
They act as basic mathematical operations.
class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
System.out.println(a+b);//15
System.out.println(a-b);//5
System.out.println(a*b);//50
System.out.println(a/b);//2
System.out.println(a%b);//0
}}
c. 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 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
}}
d. 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 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
}}
e. Logical Operators
These are used to perform “logical AND” and “logical OR” operation, i.e. the function similar to AND gate and OR gate in digital electronics. One thing to keep in mind is the second condition is not evaluated if the first one is false, i.e. it has short-circuiting effect.
&& , Logical AND : returns true when both conditions are true.
||, Logical OR : returns true if at least one condition is true.
f. Ternary Operators
The ternary operator is a shorthand version of the if-else statement. It has three operands and hence the name ternary.
g. Bitwise Operators
These are used to perform manipulation of individual bits of a number. They can be used with any of the integer types. They are used when performing the update and query operations of Binary indexed tree.& , Bitwise AND operator return bit by bit AND of input values.
|, Bitwise OR operator: returns bit by bit OR of input values.
^, Bitwise XOR operator: returns bit by bit XOR of input values.
~, Bitwise Complement Operator: This is a unary operator that returns the one’s complement representation of the input value, i.e. with all bits inverted.
h. Assignment Operator
‘=’ Assignment operator is used to assign a value to any variable. It has a right to left associativity, i.e value given on the right-hand side of the operator is assigned to the variable on the left and therefore right-hand side value must be declared before using it or should be a constant.
You may also like scope and lifetime of variables
Leave a Reply