Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Operator Precedence 
 

In Java, Operator Precedence is an evaluation order in which the operators within an expression are evaluated on the priority bases.

 

Operator Precedence

                         

Operator Precedence :

In Java, Operator Precedence is an evaluation order in which the operators within an expression are evaluated on the priority bases. Operators with a higher precedence are applied before operators with a lower precedence. 
For instance, the expression "4 + 5 * 6 / 3" will be treated as "4 + (5 * (6 / 3))" and 14 is obtained as a result.

When two operators share an operand then operator with the higher precedence gets evaluated first. However, if the operators  have the equal precedence in the same expression then that  expression will be evaluated from left to right except the assignment operators.
For example a = b = c = 15 is treated as a = (b = (c = 15)).

 The table below shows the list of operators that follow the precedence. 

 Operators  Precedence
 array index & parentheses      [ ]  (    ) 
 access object     .
 postfix  expr++  expr--
 unary  ++expr --expr  +expr  -expr  ~  !
 multiplicative  *  /  %
 additive  +  -
 bit shift   <<   >>   >>>
 relational  < >  <=  >=  instanceof
 equality  ==  !=
 bitwise AND  &
 bitwise exclusive OR  ^
 bitwise inclusive OR  |
 logical AND  &&
 logical OR  ||
 ternary  ? :
 assignment  =   +=   -=   *=   /=   %=  &=   ^=  |=    <<=  >>=  >>  >=

Lets see an example that evaluates an arithmetic expression according to the precedence order.

class PrecedenceDemo{
  public static void main(String[] args){
    int a = 6;
    int b = 5;
    int c = 10;
    float rs = 0;
    rs = a + (++b)* ((c / a)* b);
    System.out.println("The result is:" + rs);
  }
}

The expression "a+(++b)*((c/a)*b)" is evaluated from right to left. Its evaluation order depends upon the precedence order of the operators. It is shown below:

(++b)  a + (++b)*((c/a)*b)
(c/a)  a+ (++b)*((c/a)*b)
(c/a)*b  a + (++b)*((c/a)* b)
(++b)*((c/a)*b)  a + (++b)*((c/a)* b)
a+(++b)*((c/a)*b)  a+(++b)*((c/a)*b)

Output of the Program: 

C:\nisha>javac PrecedenceDemo.java

C:\nisha>java PrecedenceDemo
The result is: 42.0

 

                         

» View all related tutorials
Related Tags: c class io nested definition source while ini member e il it not init art ce in no part as

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.