What Are the Differences Between Syntax and Semantic Errors in Java?

What Are the Differences Between Syntax and Semantic Errors in Java? thumbnail
Java programs often crash when they contain semantic errors.

When you write applications in Java your code needs to observe the syntax rules of the language. This includes the text characters and punctuation symbols you use to declare variables, methods and classes. In addition to creating code syntax that is correctly structured you need to construct your programs in a way that reflects the logic at work within your application project. Semantic errors are mistakes in this logic. Both syntax and semantic errors are common problems in Java development.

  1. Java Syntax

    • Java syntax is specific and rigid unlike certain other languages in which there is a degree of flexibility. If your Java code fails to comply with the grammatical requirements of the language it will not compile or run. The following sample code contains two syntax errors:

      Strin myTerms = code program computer keyboard";

      The first error is in the data type specified for the variable being declared and instantiated in this line of code. The string data type is missing its final character. The value being assigned to the variable is also incorrectly structured as the opening set of quotes has been omitted. If you write this code in a Java IDE (Integrated Development Environment) it will fail to compile and the IDE will alert you to the syntax errors.

    Control Structures

    • The Java language provides programmers with a range of control structures to implement complex flows of execution. The following loop outline contains a syntax error:
      for(int i=0; i<10; i++ {
      System.out.println(i);
      }

      The opening section of the loop is missing a closing bracket so the compiler will fail to process the body of the loop properly. The following conditional statement section also contains an error:
      if(num<10) { int newNum = num+1; }
      System.out.println(newNum);

      This code is incorrect because the line after the conditional statement is attempting to reference a variable that is no longer in scope. As a syntax error this will prevent compilation.

    Logical Issues

    • Java programs can contain semantic errors depending on the structures and types involved. Arrays and loops often involve logical problems resulting in semantic errors as in the following sample code:
      int someNums = {3, 5, 1, 7, 2};
      for(int i=0; i<=someNums.length; i++) {
      System.out.println(someNums[i]);
      }

      The error is in the conditional test are forming part of the loop outline. The test checks that the counter variable is either less than or equal to the length of the array being iterated through. When the loop executes for the final time the code within it will attempt to access an element that is out of bounds. This is because the first index in an array is zero and the final index is one less than the array length. This code will compile and run, then fail when the loop executes.

    Error Handling

    • Syntax and semantic errors require different approaches. In most cases you will become aware of syntax errors while you write your code as your IDE will highlight them with error messages and warnings. You will not be able to compile your code until there are no syntax errors left in it. Semantic errors are more likely to appear when you are running your program. Some semantic errors become clear straight away as they cause your program to crash. However, others may only become apparent after extensive testing.

Related Searches:

References

Resources

  • Photo Credit Photodisc/Photodisc/Getty Images

Comments

Related Ads

Featured