(^140) | Arithmetic Expressions
3.10 Testing and Debugging
Testing and Debugging Hints
1.An intliteral other than 0 should not start with a zero. If it starts with zero, it is an octal (base–8)
number.
2.Watch out for integer division. The expression 47 / 100 yields 0, the integer quotient. This is one
of the major sources of wrong output in Java code.
3.When using the /and %operators with integers, remember that division by zero is not allowed.
4.Double-check every expression according to the precedence rules to confirm that the operations
are performed in the desired order, especially with expressions involving string conversion.
5.Avoid mixing integer and floating-point values in expressions. If you must mix them, use explicit
type casts to reduce the chance of mistakes.
6.For each assignment statement, verify that the expression result has the same data type as the
variable to the left of the assignment operator (=). If not, use an explicit type cast for clarity and
safety. Also, remember that storing a floating-point value into an intvariable truncates the frac-
tional part.
7.If an application is producing erroneous results and all of its expressions appear to be correct,
check whether any of them can result in integer overflow. Also, check whether they contain any
unintentional type conversions.
8.For every library package you use in your application, be sure to use an importdeclaration.
9.Examine each method call to confirm that you have the right number of arguments and that the
data types of the arguments are correct.
10.Remember to return an expression from a value-returning method. The expression must produce
the same type as the result type specified in the method heading.
11.Keep your code neatly formatted so that it is easier to read.
12.If the cause of an error in your code is not obvious, leave the computer and study a printed listing.
Change your code only after you understand the source of the error.
13.Remember to use a class name with a class method, and an object name with an instance
method.
14.You cannot input numeric values directly. Use one of the parsemethods to convert an input
string to a numeric value.
15.Be careful to type valid numeric values as input to an application that reads numbers. At this
point in our knowledge of Java, entering an incorrectly formed number will cause the application
to crash with an error message.
T
E
A
M
F
L
Y
Team-Fly®