Translate

Search This Blog

Total Pageviews

Showing posts with label operators. Show all posts
Showing posts with label operators. Show all posts

Wednesday, October 10, 2018

Java Operators Precedence Rules

Advice: in case of dubt don’t confuse yourself or the reader of your program; use parentheses liberally.


If you use several operators in one expression, and if you don’t use parentheses to explicitly indicate the order of evaluation, then you have to worry about the precedence rules that determine the order of evaluation.

Here is a listing of the operators, listed in order from highest precedence (evaluated first) to lowest precedence (evaluated last):

Unary operators:  ++ --, !, unary -, unary +, type-cast
Multiplication and division: *,  /, %
Addition and subtraction: +, -
Relational operators: <, >, <=, >=
Equality and inequality: ==, !=
Boolean and:  &&
Boolean or:  ||
Conditional operator: ?:
Assignment operators: =, +=, -=, *=, /=, %=

Operators on the same line have the same precedence.
When operators of the same precedence are strung together in the absence of parentheses the rules of associativity are, unary operators and assignment operators are evaluated right-to-left, while the remaining operators are evaluated left-to-right. 
For example,
A*B/C means (A*B)/C, while
A=B=C means A=(B=C)
(Can you see how the expression A=B=C
might be useful, given that the value of B=C as an expression is the same as the value that is
assigned to B?)


Resources

Introduction to Programming Using Java - David J. Eck

Java and Relational Operators

These operators can be used to compare values of any of the numeric types.
They can also be used to compare values of type char. For characters, < and > are defined according the numeric Unicode values of the characters.
(This might not always be what you want. It is not the same as alphabetical order because all the upper case letters come before all the lower case letters.)
You can also assign boolean-valued expressions to boolean variables, just as you can assign numeric values to numeric variables. And functions can return boolean values.
By the way, the operators == and != can be used to compare boolean values too. This is occasionally useful. For example:

boolean sameSign;
sameSign = ( (x > 0) == (y > 0) );


One thing that you cannot do with the relational operators <, >, <=, and >= is to use them to compare values of type String.
You can legally use == and != to compare Strings, but because of peculiarities in the way objects behave, they might not give the results you want.
(The == operator checks whether two objects are stored in the same memory location, rather than whether they contain the same value. Occasionally, for some objects, you do want to make such a check—but rarely for strings.) 

Instead, you should compare strings using subroutines such as equals() and compareTo()

Another place where == and != don’t work as you would expect is with Double.NaN, the constant that represents an undefined value of type double.
The values of x == Double.NaN and x != Double.NaN are both defined to be false in all cases, whether or not x is Double.NaN! 
To test whether a real value x is the undefined value Double.NaN, use the boolean-valued function Double.isNaN(x).


Resources

Introduction to Programming Using Java - David J. Eck