Operator Precedence in programming languages

by Gabriela C. Perez.

Share
|
Homepage | Submit your article | Contact | TOS
More articles on java  

You are here: Categories » Travel and leisure » JAVA

Operator precedence deciphers the order in which calculations in an expression occur. Looking at the calculation example 3 + 4 * 6, the answer could be calculated by adding 3 and 4, which gives 7, and then multiplying 7 by 6, giving the answer of 42. However, we could also multiply 4 and 6 first, which gives 24, and then add on the 3, giving an answer of 27. The multiplication operator (*) actually has a higher precedence than the addition operator (+). This means that the numeric expression 3 + 4 * 6 would actually give the answer 27 and not 42, executing the multiplication first and then the addition. In order to specify the order in which calculations occur you can simply use parentheses. If we want the addition calculation to be executed before the multiplication, we can enclose the addition calculation in parentheses (e.g., (3 + 4) * 6, which will give us the answer 42). When in doubt, it is recommended that you use parentheses to specify the order of operations. It is often best to use parentheses anyway to make your code more understandable.

The following table shows an operator list containing operators with a higher precedence at the top and thoses with a lower precedence at the bottom. The table also shows the associativity of grouped operators that are of equal precedence. The associativity deciphers the order of operators of equal precedence. For example, division has a "left" associativity, which you may look upon as being left to right. This means that the expression 24 / 4 / 2 would be the same as (24 / 4) / 2, equaling 3, and would not be the same as the expression 24 / (4 /2), which gives a result of 12. Here is the operator precedence table and the associativity of operators of equal precedence.

Operator Group Associativity
(), [], ., postfix++, postfix–– Left
+ unary, – unary, ++prefix, ––prefix, ~, ! Right
new, (cast) Left
*, /, % Left
+, – Left
<<, >>, >>> Left
<, <=, >, >=, instanceof Left
==, != Left
& Left
^ Left
| Left
&& Left
|| Left
?: Left
=, *=, /=, %=, +=, –=, <<=, >>=, >>>=, &=, |=, ^= Right

Thinking back to the two examples that we have looked at so far, we can first see that the multiplication operator is higher up the table than the addition operator, meaning it has a higher precedence. We can also see that the division operator has a left (left to right) associativity, as we previously discussed.

Leave a comment or ask a question
Total comments: 0

JAVA Disclaimer

  • The e-articles directory is not responsible for any and all copyright infringements by writers and authors. If you suspect the information contained by this page for any copyright infringements, please contact us to investigate the issue
JAVA and its Advantages - In today's highly competitive world, JAVA has become one of the most secure technologies for website and software development. Prime benefits of JAVA are platform independency and easy availability (more...)
Java Tactics: Earn A Fortune With Java - Get the right Knowledge Get the right Papers Get the right Direction Get the Money Hi, my name's (more...)
Introduction to Object Oriented Programming (OOP) - The transition from a procedural programming (non-OOP) language to an object-oriented programming language is a large step for many programmers. It is true that both methods of programming can ulti (more...)
Arithmetic Assignment Operators - The following assignment operators are similar to the increment and decrement operators that we have just seen. They are used so that you do not need to enter the source variable twice when ass (more...)
Bitwise Operators - The following table shows the standard bitwise operators in Java and a description of them. (more...)
Invocation Chaining - Invocation chaining means that you are not limited to merely accessing one class/object member in a given statement with the . operator but may continue to access further members in a given stateme (more...)
Regular Expressions in Java - A regular expression is a code that is used to match a pattern in a given string and is new to Java 1.4. Regular expressions are made up of normal characters and metacharacters. Normal characters a (more...)
Character Escape Sequences - Character escape sequences allow for a character to be interpreted differently than its literal value. Character escape sequences are defined using the backslash (\) character, followed by th (more...)
Conditional Statements - The ability to choose the path that your program takes, based on any given data, is the key to all functionality in programming. In order to create conditional statements, we must first learn a (more...)
Java Methods - Methods are used as the building blocks of your program, performing tasks that can be called again and again and using the same code to perform the task each time. The basic but fundamental parts o (more...)

 
free content
    Copyright © 2006 - 2012 e-articles.info.
The texts, articles and tutorials in the directory are property of their respective owners and authors.