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 compiler and therefore the compiler can show you where (sometimes exactly where, sometimes approximately where) the error occurs.
Guidelines for Correcting Logic Errors (Run-time Errors)
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);
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.
= = 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.
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.
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);
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.
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.