Troubleshooting

   

Expression Syntax Errors

The compiler reports a ')'expected error in an expression.

Syntax errors often result from mismatched parentheses in an expression. When an expression cannot be compiled, verify that the parentheses are correctly paired. If an expression is so complex that this is difficult to do, consider separating it into multiple statements that assign intermediate results to variables that are then used to compute the desired result. This approach will reduce the likelihood of syntax errors and improve the maintainability of your code.

Unexpected Expression Results

An expression produces a numeric result other than what you expected.

First, review the precedence of Java operators in Table 5.2 to determine if the expression operators are being evaluated in an order other than what you intended. If this is the case, add parentheses to the expression as needed to correctly identify the desired operands. Also, examine the expression for any integer division that might introduce unintended truncation of intermediate results.

Possible Loss of Precision

You get a compiler error warning you that a statement might result in a loss of precision.

This error results from an attempt to perform a narrowing conversion, such as assigning the result of an expression that operates on double values to a float variable, without an explicit cast. You can address this error by explicitly casting the expression result to the type of the target variable in the assignment. However, you should only do this if you are certain that the target variable can hold the magnitude of the result and that the resulting precision is acceptable. For example, you should not cast a long result to an int if the magnitude of the result might exceed the storage capacity of an int. You should instead change the type of the target variable to support what is required.

   


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