|
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
Java ExpressionsExpressions 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 its 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 youd 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 OperatorsBecause Java expressions are typically made up of several subexpressions linked together by operators, its 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 PrecedenceExpressions 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 couldnt 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. Youll see that Ive placed parentheses first in this table. Parentheses arent really operators because they dont perform an operation on data types, but they appear in the table nonetheless to emphasize that theyre given top priority during evaluation.
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:
|
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. |