hts.StevenWood.com

Logic Errors

Home ] Up ] Machine Code ] Simple Program ] Assignment 2-2 ] Syntax Errors ] [ Logic Errors ] Assignment ][ Last Page ]

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)

  1. 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
  2. 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.
  3. 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.
  4. 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.
  5. 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.
    1. Examine the call stack and the values of parameters and local variables. See if it matches your expectation of what the program "should" do.
    2. 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.
  6. 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

Home ] Up ] Machine Code ] Simple Program ] Assignment 2-2 ] Syntax Errors ] [ Logic Errors ] Assignment ][Last Page]
All material on this site is copyright © 1997- by Steven Wood or as credited. All right reserved. Use of this site indicates you agreement with the  terms of use.
Send comments or questions to about this page.
Every attempt has been made to credit work under copyright. If there are any claims that copyright has been missed please contact the .
+SDG+