By John Paul Mueller

Logical errors in Java programming can be extremely difficult to find because they don’t reflect any sort of coding problem or an error in the use of Java language elements. The code runs perfectly as written — it just isn’t performing the task that you expected it to perform.

As a result, logical errors can be the hardest errors to find. You need to spend time going through your code looking for a precise reason for the error. Here’s a list of common logical errors that Java developers encounter:

  • Using incorrect operator precedence: The order in which Java interprets operators is important. Applications often produce the wrong result because the developer didn’t include parentheses in the correct places.

    For example, the following example produces outputs of 11, 13, 9, and 8 from the four variables, all due to the location (or lack) of the parentheses.

    public class OperatorError
    {
       public static void main(String[] args)
       {
          // Create some variables.
          int MyVar1 = 5 + 4 * 3 / 2;
          int MyVar2 = (5 + 4) * 3 / 2;
          int MyVar3 = (5 + 4) * (3 / 2);
          int MyVar4 = (5 + (4 * 3)) / 2;
          // Output the result.
          System.out.println(
                "MyVar1: " + MyVar1 +
                "nMyVar2: " + MyVar2 +
                "nMyVar3: " + MyVar3 +
                "nMyVar4: " + MyVar4);
       }
    }
  • Defining the wrong count: Possibly the most common logical error is counting things incorrectly. People are used to starting counts with 1, and computers often start counts with 0. So, it’s not uncommon to find that applications are precisely one off in performing a task, whether that task is running a loop or working with a collection of items.

  • Assuming a condition is true when it isn’t: Developers will often look at the statement used to define a condition and assume that the statement is true (or false) without verifying the logic of the statement.

    The use of an or statement when you really meant to use an and statement can also cause problems. The logic employed to make decisions causes many developers, even experienced developers, a lot of problems. Always verify your assumptions for conditional statements.

  • Relying on floating point numbers for precision work: You can’t assume that floating point numbers will deliver a specific number. This means you can’t check a floating point number for equality to any specific value — you must instead use a range of values to perform the check. Floating point numbers are always an approximation in Java.

  • Relying on integer values to measure values: Integers are great for counting items because they’re precise. However, many integer math operations create imprecise results. This is especially true for division, because the remainder is always left off. (The number is rounded down.) Use floating point values or the BigDecimal object type when you need to measure something and precision is important.

  • Misplacing a semicolon: It’s possible to create Java code that compiles and runs perfectly well despite having a semicolon in the wrong place. Here’s an example:

    public class ForLoopError
    {
       public static void main(String[] args)
       {
          // Declare the variable.
          int Count;
          // Create the loop.
          for (Count=1; Count<=10; Count++) ;
          {
             // Output the result.
             System.out.println("Count is " + Count);
          }
       }
    }

    Notice that the semicolon appears immediately after the for statement, rather than after the code block as it should. Instead of printing individual values of Count, this example prints a single sentence that says Count is 11.