Operators and Assignments Part-5

Operators in Java

Mainly the categorization of operators can be in the  following types:

new Operator

In java we use this operator to create an object. This object can be of class or even array. So by using new operator,compiler performs memory allocation to the variable. Unlike other languages, this memory allocation is deallocated automatically by Garbage Collector, hence there is no delete operator present in java. For example,

int []a = new int[10];
String msg = new String("Hello world");
Object o = new Object();

Firstly when we are creating object of classes, a call to constructor follows after the new operator, which basically initializes the new object.


Subscript Operator

Representation of subscript operator is done by []. We can use this operator for the declaration and creation of arrays. For example,

int []a;                  //declaring array
a = new int[10];          //creating array

 


Operator Precedence

Usually for almost all different types of operators, their precedence are also likewise different. Precedence for operator according to their types is as follows:
Unary Operator:

[], i++, i--                    //high priority
++i, --i, ~, !
new                             //least priority

Note- Operators in the same line are of equal precedence.
Arithmetic Operator:

*, /, %     //higher priority
+, -        //lower priority

Note- Operators in the same line are of equal precedence.
Shift Operator:

>>>, >>, <<

Comparison Operator:

<, <=, >, >=, instanceof

Equality Operator:

==, !=

Bit-wise Operator:

&   //highest precedence
^
|   //least precedence

Short Circuit Operator:

&&
||

Conditional Operator:

?:

Assignment Operator:

=, +=, -=, *=, /=

 


Evaluation Order of Java Operands

In java there is a certain pre defined way to evaluate different operator. Almost all the binary operator are evaluated from left to right while above all the assignment operator are evaluates from right to left. So let us now consider the following example,

public class Test
{
public static void main(String []args)
{
System.out.println(a(1)+a(2)*a(3)+a(4)*a(5)/a(6));
}
public static int a(int i)
{
System.out.print(i);
return i;
}
}

Output:

10

Explaination:

1+2*3+4*5/6      //step 1
1+6+4*5/6        //step 2
1+6+20/6         //step 3
1+6+3            //step 4
10               //step 5

Consider example 2:

int a = 10;
int b,c,d,e;
b = ++a;                   //operation 1, incrementing value of a and then assigning it to b
System.out.println("a = "+a); 
System.out.println("b = "+b);
c = a++;                  //operation 2, assigning value of a to b and then incrementing a
System.out.println("a = "+a); 
System.out.println("c = "+c);
d = ++a + a++ + a++ + ++a;     //operation 3
System.out.println("a = "+a); 
System.out.println("d = "+d);
e = 0;
e += ++a + a++;               //operation 4
System.out.println("a = "+a);
System.out.println("e = "+e);

Output:

a = 11
b = 11
a = 12
c = 11
a = 16
d = 56
a = 18
e = 34

 

Leave a Comment

Your email address will not be published. Required fields are marked *