Troubleshooting

   

Two Equivalent Objects Are Not Equal

An equality comparison between two object references using == fails even though the objects are equivalent.

When used with reference types, the == operator returns true only if the two references point to the same object. To test for object equivalence, you must use the equals method as declared in the associated class instead of == . If equals has not already been overridden for the class, you must implement an override that returns true if two instances of the class are equivalent relative to the needs of your program. For example, equals is overridden for String to return true if two String objects hold the same sequence of characters .

Mismatched if and else Statements

The blocks executed within an if-else statement are not those you intended.

This error typically results from a mismatch of if-else pairs in a control statement. Remember that indentation plays no role in how else statements are matched with if statements. Place braces around the statement blocks associated with each if and else statement you are examining to clearly identify how they should be matched.

Incompatible Type for Conditional Clause of a Control Statement

The compiler reports an error stating that the variable or expression you have specified in the conditional clause of a control statement, such as an if or while statement, is of an incompatible type.

The Java compiler is exceedingly strict regarding the use of boolean expressions. The expression you supply to a conditional clause must evaluate to a boolean result. If you are attempting to control execution based on an integer value or an object reference, build your control expression by comparing the integer variable for equality with 0 or the object reference with null instead of attempting to use either directly.

for or while Loop Never Executes

The block associated with a for or while loop never executes although you expected it to execute at least once.

The conditional clause of a for or while loop is evaluated prior to the first iteration of the loop. If this condition is false , the loop is never entered. You should use a do loop if you require a loop to execute once regardless of the value of the conditional expression.

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net