Home

Semantic Errors in Java

|
|  Updated:  
2016-03-26 12:49:16
|   From The Book:  
No items found.
Java Essentials For Dummies
Explore Book
Buy On Amazon

Many people find the difference between semantic errors and syntactical (syntax) errors in Java code hard to understand, but they are different. You can see a semantic error when the syntax of your code is correct but the code usage isn’t correct.

The most common semantic error is one in which the code uses a variable that isn’t initialized properly. Fortunately, the compiler finds this particular semantic error in most cases. Here’s a list of other common semantic errors you need to know about.

  • Using an operator that doesn’t apply: In some situations, you might try to use an operator that doesn’t apply to the variable or variables in question. For example, you can’t use the increment operator (++) with a boolean variable.

    Newer versions of Java have become adept at finding this error, but you can still encounter difficulty figuring out precisely why an error occurs when you use the wrong operator in some cases. For example, MyObj1 == MyObj2 won’t compare the two objects — the equality operator works only with primitive types.

  • Using incompatible types: This type of semantic error can be tricky because the compiler will flag some errors and not others. For example, if you try to assign a float to an int variable, the compiler displays an error message.

    On the other hand, if you assign an int to a float variable, the compiler performs an automatic type conversion to the int to make it a float. The problem with this second scenario is that it can silently introduce errors in your code, especially if you really did mean to use a float.

  • Losing precision during a conversion: Sometimes you can apply casting incorrectly. For example, casting a float to an int works fine, but at the loss of the decimal portion of the number.

    This loss of precision can affect the output of your application in unexpected ways and cause the output to reflect a value other than the one you expected. The compiler never finds this sort of error because you have specifically applied a cast to the variable and the compiler expects that you know what you’re doing.

  • Performing an impossible cast: It’s possible to convert between many different types in Java. However, no matter how much you’d like to convert a boolean value into an int, Java won’t let you do it. The concept of performing the cast is syntactically correct, but you’re applying it incorrectly, making this a semantic error that the compiler always catches.

  • Applying scoping incorrectly: Any variable you declare inside a method has the same scope — visibility to other parts of the application, in other words — as the method. Consequently, you can’t declare a private static int variable inside a method. Instead, you must define the variable globally like this:

    public class PrivateVar
    {
       // This declaration works.
       private static int PrivateInt = 3;
       public static void main(String[] args)
       {
          // This declaration doesn’t work.
          private static int PrivateInt = 3;
       }
    }

These are the most common problems you should look for in your code. Semantic errors tend to be harder to find than syntactical errors, but not nearly as hard as logical errors.

About This Article

This article is from the book: 

No items found.

About the book author:

John Paul Mueller is a freelance author and technical editor. He has writing in his blood, having produced 100 books and more than 600 articles to date. The topics range from networking to home security and from database management to heads-down programming. John has provided technical services to both Data Based Advisor and Coast Compute magazines.