Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Sams Teach Yourself Visual J++ 6 in 21 Days
(Publisher: Macmillan Computer Publishing)
Author(s): Rick Leinecker
ISBN: 0672313510
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Java Expressions

Expressions are statements that, when executed, result in a value. When programming, you use expressions all the time, sometimes without even realizing it. Java expressions are similar in syntax to those of C and C++. The following are examples of Java expressions:

65 + 5 // Produces a value of 70.
( i < 10) // Produces a true or false.
5 * 100 // Produces a value of 500
x = 25 – 5 // Subtracts 5 from 25 then stores in x
175 – 4 // Produces 171

Expressions are typically composed of several smaller expressions, or subexpressions, connected by operators. For instance, consider the following lines of code:

int x = 100, y;
y = ( x / 4) + 4;

The first line of code, in which the variables are declared, actually contains an expression in the assignment of 100 to the x variable. The integer literal 100 is an expression, albeit a simple one. When the compiler looks at this line, it sees something like “evaluate the expression to the right of the assignment operator = and place its value in the variable on the left.” Because the expression is the integer literal 100, it evaluates to 100 and is then stored in the x variable. Pretty simple, yet it’s an expression.

Now take a look at the next line. The complete expression would be this:

y = ( x / 4) + 3;

However, this expression is made up of several subexpressions. You might recognize one right off the bat:

x / 4

Although you’d be correct in assuming this to be a subexpression, there are even subexpressions inside this simple statement. The x variable is a subexpression that evaluates to 100, and the integer literal 4 is another subexpression that evaluates to 4. After each of these is evaluated and the division operation is performed, the result is 25. To this, the value 3 is added, another subexpression. And finally, the entire value (28) is placed inside the y variable. As you can see, there can be many levels of expressions, even in what appear to be simple statements.

Understanding Operators

Because Java expressions are typically made up of several subexpressions linked together by operators, it’s important to understand exactly how operators work. Java supports both unary and binary operators. Unary operators are those that act on a single operand, whereas binary operators act on two operands.

The following example is an expression using the unary postfix increment operator. As you can see, it requires only the one operand (x, in this case):

x++;

And here is the functional equivalent to the preceding using the binary addition operator:

x = x + 1;

As you can see, the binary addition operator acts on two operands (x and 1). After the right side of the assignment operator is evaluated, the result is stored in the x variable on the left.

These two expressions do the same thing: they increase the value of x by one. However, a different operator is used in each case. In the first example, the unary operator ++ is used. In the second example, the binary addition operator + is used.

Evaluating Operator Precedence

Expressions are evaluated from left to right, according to the precedence of the operators in the expression. By following the precedence order, Java guarantees that a particular expression will produce the same results every time it is executed. For example, take into consideration the following example:

x = 15 + 3 * 2 – 14;

Without a precedence order, which subexpression is evaluated first? Is it 15 + 3, 3 * 2, or 2 – 14? And after the first subexpression is evaluated, which is next? Clearly, the value of the expression changes, depending on the order in which its subexpressions are evaluated.

If you couldn’t rely on the order in which operations are performed, your programs would be about as consistent as the New York City subway system. Luckily, Java operators are arranged and executed in order of precedence. Table 2.3 lists all Java operators according to their precedence order. You’ll see that I’ve placed parentheses first in this table. Parentheses aren’t really operators because they don’t perform an operation on data types, but they appear in the table nonetheless to emphasize that they’re given top priority during evaluation.

Table 2.3 Java Operator Precedence

Precedence Associativity Operator Description

First () parentheses (forcing order)
Second R-to-L ++ pre/post increment (unary)
R-to-L -- pre/post decrement (unary)
R-to-L ! logical complement (unary)
R-to-L unary bitwise logical negation
R-to-L + addition (unary)
R-to-L - subtraction (unary)
Third L-to-R */% multiplication, division, and remainder
Fourth L-to-R + - addition and subtraction
Fifth L-to-R << shift left
L-to-R >> shift right (sign extension)
L-to-R >>> shift right (zero extension)
Sixth L-to-R < <= “less than” and “less than or equal to”
L-to-R > >= “greater than” and “greater than or equal to”
L-to-R instanceof “is the object an instance of this class?”
Seventh L-to-R == equality
L-to-R != non-equality
Eighth L-to-R & bitwise AND, boolean AND
Ninth L-to-R ^ bitwise XOR, boolean XOR
Tenth L-to-R | bitwise OR, boolean OR
Eleventh L-to-R && AND (boolean conditional “short-circuit”)
Twelfth L-to-R || OR (boolean conditional “short-circuit”)
Thirteenth R-to-L ?: ternary conditional
Fourteenth R-to-L = assignment
Fifteenth R-to-L += -=
*=/= %=
assignment (and operation)

Because all operations are performed according to the precedence of the operators involved, Java expressions are evaluated in a predictable manner. Operations whose operators have the highest precedence are performed first, with lower-precedence operators following in sequence. When operations of the same precedence occur within the same expression, they are processed from left to right.

Now, return to the example:

x = 15 + 3 * 2 – 14;

Thanks to operator precedence, you can see that the expression evaluates in a specific order. The middle operation (3 * 2) is performed first, and then the next two operations are carried out from left to right:

3 * 2 produces 6, which is used in the next operation
15 + 6 produces 21, which is used in the next operation
21 – 14 produces 7, which is stored in the variable x


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.