|
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
Forcing Operator PrecedenceIf you dont want the natural order of evaluation to be used, you can use parentheses to control the order of evaluation. For example, suppose you wanted the subexpression 2 14 to be evaluated first. You could ensure this by placing it inside of parentheses: x = 15 + 3 * ( 2 14); In this case, the following sequence of steps is taken when the expression is evaluated:
Because multiplication operators have precedence over arithmetic ones, this operation is performed after the subexpression inside parentheses. However, you could have added another pair of parentheses to force the multiplication operation to take place last: x = ( 15 + 3) * ( 2 14); In this case, the subexpressions inside the parentheses are of the same precedence. As a result, they are carried out from left to right. This sequence of steps is as follows:
And, of course, parentheses can be nested. You can, for example, group these three subexpressions by surrounding them with parentheses and adding another subexpression to the mix. Here are a few examples, each producing a different data type: x = ( ( 15 + 3) * ( 2 14)) + 1; // Produces integer value x = ( ( 15 + 3) * ( 2 14)) + 1.2; // Produces a floating-point value x = ( ( 15 + 3) * ( 2 14)) > 1; // Produces a boolean value Performing Array OperationsWhen operations are performed on arrays, they return the value of a specific element in that array. However, unlike the data types dealt with thus far, an array element must be allocated using the new operator before it can be assigned to a variable: int a = new int[15]; In the preceding example, an array of 15 integer elements is created and assigned to the variable a. After this operation has taken place, you can store and retrieve values in the array elements using the following syntax: nArrayVariable[expression] For example, each of the following lines of code accesses the same element in our array: int x = 5, y = 2, z = 10; // Initialize variables. a[10] = 82569; // Store 82569 in the 11th element. a[z] = 4370; // Store 4370 in the 11th element. a[x*y] = 1117911; // Store 1117911 in the 11th element. a[x+5] = 592; // Store 592 in the 11th element. int i; i = a[10]; // Retrieve the 11th element. i = a[x+5]; // Retrieve the 11th element. i = a[100/z+1]; // Retrieve the 11th element. i = a[a.length-5]; // Retrieve the 11th element. In the preceding example, I used the instance variable length in the expression. Because a length returns the number of elements in the array, this is a valid expression. If, in any case, an array index is negative or greater than the number of elements in the array minus one (which would be 14 in this example), an ArrayIndex OutOfBoundsException is thrown. Using the sample array, the following operations would cause an out-of-bounds exception: a[x+2]; // Expression evaluates to 15 a[x*x]; // Expression evaluates to 25 a[a.length]; // Expression evaluates to 15 Array index values can be of byte, short, int, or event char types. However, array indexes of type long are not permitted. To use a long, it must be cast into the int type, as the following example shows: long lMyLong = 10; a[lMyLong]; // Illegal since it's a long. a[(int)lMyLong]; // Legal since it's cast to an int. Keep in mind that array indexing begins at zero. In this example, as with all arrays, the first element is accessed with an index value of zero: a[0]. Because there are 15 elements in this particular array (from index 0 to 14), the last element is referenced with an index value of 14: a[14]. In Java, a special operator exists that allows you to determine whether an object is an instance of a particular class, subclass, or interface. Using the instance of binary operator, you can test objects to compare them against a specific class or interface type: if( theObject instance of className) { // Do stuff }
|
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. |