|
| |
Common Logic Errors in Java
(run time errors)
Debugging is the process of
correcting Run-Time Errors. Debugging is much more difficult than correcting
syntax errors because you often don't know where in the source the error exists.
Syntax errors are caught by the complier and therefore the complier can show you
where (sometimes exactly where, sometimes approximately where) the error occurs.
Guidelines for Correcting Logic Errors (Run-time Errors)
- Avoid the debugging process. Taking the time to write a correct and
well-documented program from the start of the programming process will save
you time in the long run. Finding and correcting program "bugs"
(logic errors) takes a lot of time, and it can be frustrating, especially if
you neglected to include code comments
- Debug on a small scale. Thoroughly test each component of an application
(each method of each object) piece by piece, correcting errors as you go.
This is much easier than finding errors in a large program.
- Understand the symptom. Before changing a program, understand why the
error occurred and where in the program it may have occurred. Look at what
the program tried to do and where it went wrong.
- Use problem solving techniques to try to determine why an error happened
how it could have arisen. Form several hypotheses about how the error might
have arisen and explore these possibilities.
- Find the source of the error (find the root cause). Don't
waste time making "random" changes to your program. After
brainstorming about the cause of the error, you can start searching
back through the execution of your program to see where things went wrong.
A debugger can help.
- Examine the call stack and the values of parameters and local
variables. See if it matches your expectation of what the program
"should" do.
- Trace through an execution of your program by hand and with a
debugger, stopping at certain points in the execution and/or printing
out values along the way.
- Correct the errors and test your program thoroughly using various test
cases.
The Most Common Logic Errors in Java:
Using a variable before it is given a value
This is a common error found in both object-oriented and procedural languages.
In Java, primitive variables must be initialized to zero or some default
value so there will be no doubt as to what is stored in that variable.
For Example, the following code ...
int x;
x = x + 1;
System.out.println("X = " + x);
... would have unpredictable results (the value of x is some random
number)
To fix the problem, initialize the value stored in x to a known value (like
0) ...
int x = 0;
x = x + 1;
System.out.println("X = " + x);
Misplaced Semi-colon (usually with a loop or if statement)
This is a common error that is done by beginning Java programmers.
For example:
FOR LOOP:
for (i=1; i<=10; i++) ; {
System.out.println("Number is " + i);
}
The above code will print out "Number is 11"
instead of the desired ...
"Number is 1"
"Number is 2"
...
"Number is 10"
IF:
if ( x > y) ; {
System.out.println("X is bigger");
}
The above code will print out "X is bigger"
regardless of what values are stored in x and y.
Confusing the equivalence operator == with
the assignment operator =
= = is used to compare two values to see if they are the same while = is used
to assign a value to a variable.
Be careful though, when the = = operator is applied to objects (like
stings, arrays, ...) then it compares the addresses of those objects
rather than what is stored in those objects.
For example, the if statement ...
String X1="me";
String X2="me";
if(X1 == X2)
{
...
}
... will execute the code denoted by the three dots only if the first
object occupies the same address as the second object. If the objects
occupied different addresses, but still had the same values stored in
variables, then the "is statement" would evaluate to false.
Unfortunately this does not give rise to any syntax errors, but will show up
when any program containing the error is executed.
Missing the "main" method
All java applications must have a main( ) method that has the following form
...
public static void main (String []args) {
}
If you mistype any part of this line or miss out a keyword, then a run-time
error will be generated.
For example, if you omit the keyword static then an error
message of the form:
Exception in thread main.....
will be generated at run time.
Math: Order of Operation (BEDMAS)
The Java programming language follows the Mathematic order of operations (BEDMAS)
and it is a common programming error to omit brackets when doing math
opeartions.
For example:
x = a * b + c;
may result in a different value stored in x then would ...
x = a * (b + c);
Forgetting that primitive types (int, double, char ...) are passed by
value
You cannot treat an argument which is a primitive type as if it can be
assigned to. This will not be signalled as a syntax error. However, it will
show up as a run-time error when you write code which assumes that the scalar
has been given a value by a method.
Forgetting that arguments are passed by reference to methods if they are
objects (like Strings and arrays)
When an object is used as an argument to a method, then its address is passed
over and not a value. This means that you can assign values to such arguments.
If you treat them as values this will not strictly be an error, but will not
be making use of the full facilities of an object-oriented programming
language.
Source: TVDSB |