Tuesday, 3 June 2008

Overview of the Java Operators

Overview of the Java Operators

Java’s operators, which are shown below, perform traditional arithmetic and logical operations,as well as the object-oriented cast and instanceof operations. They are listed in precedence order, with the highest precedence at the top of the table. Each group has been given a name for reference purposes; that name is shown in the left column of the table. Arithmetic and comparison operators are each split further into two subgroupings because they have different levels of precedence.

Operators in Java, in Descending Order of Precedence











Category Operators
Unary ++ -- + - ! ~(type)
Arithmetic * / % + -
Shift << >> >>>
Comparison < <= > >= instanceof == !=
Bitwise & ^
Short-circuit &&
Conditional ?:
Assignment = op=


Hence,looking into table above programmers should be aware of the outputs of the below programs...


package javaOperators;

public class Eval7 {

public static void main(String[] args) {
int i=10;
int j=5;
int k=1;
int l=i=i+1&j+i&k++;
System.out.println(l);
System.out.println(i);
System.out.println(j);
System.out.println(k);
}
}



Prints:
1
1
5
2


int i=1;
int j=10;
int k=5;

int sum=j&i++*k+i|j;
System.out.println(sum);


Prints 10.


int i=9;
int j=10;

int k=++j-i-->>i&j%i;
System.out.println(k);
System.out.println(i);
System.out.println(j);


Prints
0
8
11

Hope this explanation clears any basic doubts if any regarding operator order of precedence.

As always,google search can do the trick also.

No comments: