Operators in Java

Java operators are used in performing different operations with variables by manipulating them. Java supports a total of 44 numbers of operators.

Java Operators - Different types of Operators in Java

java operator

 

The Shift Operator

java operator shift operator

An example worth looking at:

500 shift by 2 (500 >>2) = 500/(2*2)=125 (here shift-bits=2, take only the integer parts after division)

Another example:

public class Demo {public class Demo {
 public static void main(String[] args) { 
System.out.println(10 >> 2); // 2 
System.out.println(10 << 2); // 40 
System.out.println(-10 << 2); // -40 
System.out.println(-10 >> 2); // -3 
System.out.println(-24 >> 3); // -3 
System.out.println(-10 >>> 28); // 15 
}
}

The Unary Operator

public class Test {
public static void main(String[] args) {
boolean w=true;
System.out.println(!w);//false, opposite of the boolean value
	int a=5;
	int b=0;
	b=a++ + ++a + a-- + --a + ++a; //5+7+7+5+6, where a++ = a+1
	/*for ++a the number is first incremented then stored but 
	for a++ number is first stored then incremented*/
	System.out.println(a+"\t"+b);//a=6, b=30
}
	
}

The Relational, Arithmetic, Logical, Bitwise & Assignment Operators

  • All relational operators return a boolean value i,e. true or false. 
  • The Logical operator checks the first condition, if it returns false then only it would check for the second condition.
  • The Bitwise operators always check both the conditions irrespective of the result of the first condition.
  • Assignment operators are used for assigning a value to a variable. All assignment operators evaluate from right to left.
  • Arithmetic operators are used mostly to perform simple arithmetic operations like addition, subtraction etc.
public class Test {
public static void main(String[] args) {
System.out.println(10==2); //false

System.out.println(10%3);//1, only reminder is taken

System.out.println(10<4 && 10<20); //false as 10<4 is true and 10<20 is false 
/* In logical AND both conditions
 needs to be true to return a true.
*/
System.out.println(10<4 || 10<20); //true, 
//In logical OR if any one of the conditions return true the final result will be true. 

int x=10; 
x+=4; //x=x=4 
x>>=2; //x=x>>2 (shift operator)
x*=2; //x=x*2
System.out.println(x); //6

int k,i,j; //, is used for seperator

int a=2, b=3,c=4,d=5;
System.out.println((c&d)==5); //false
System.out.println((c|d)==5); //true

a=b=c=d; //evaluation is done from right to left
System.out.println(a); //5
}
	
}

The Ternary Operator

public class Test {
public static void main(String[] args) {

int result=10>20?5:2;//used to check greater between two numbers
System.out.println(result);//2,
//if 10 is greater then it will print 5 else it will print 2 
}
	
}
public class A {

}
public class B extends A {

}
public class Test {
public static void main(String[] args) {
A obj1=new A();
B obj2=new B();
System.out.println(obj1 instanceof A); //true
System.out.println(obj2 instanceof B); //true
System.out.println(obj2 instanceof A); //true
System.out.println(obj1 instanceof B); //false

}	
}

The Dot(.) Operator

It is used to access the:

  • variable
  • method
  • constant
  • object
  • package
  • class



public class Test {
public static void main(String[] args) {
	/*the dot operator is used to call the package name lang, class System,
	object out and variable length, SIZE*/
java.lang.System.out.println(args.length);
java.lang.System.out.println(Integer.SIZE);

}	
}

The instanceOf Operator

This operator always checks whether the object belongs to the same class or its child class and returns a boolean value. In the below example B is the child class and A is the parent class.

A better way to understand the above scenario in depth:

 

java operators

The concept is that a child class object can always access the resource of itself as well as its parent's. However, a parent class object can access the resource of itself only. This way of accessing resource is called Inheritance. We will discuss it in detail in later parts of the tutorial.