Pitfalls to avoid for new Java programmers.
If you have been around for any amount of time as a Java programmer, you will have made all of the following errors at least a few times. Programming errors in Java, as well as most languages, are either syntax errors or logic errors. Syntax errors are caused when the Java compiler (javac) cannot recognize a statement. This causes the compiler to return an error message, usually with a line of code to reference. Another name for syntax errors is compile-time errors.
Logic errors are bugs. These are the insidious fellows that you will spend the rest of your programming career tracking down. If given a choice any programmer will choose compile-time errors, hands down. I have spent days at a time debugging tough logic errors using all the tools available to me: logs, System.out messages, debuggers, and my grey-matter.
The following errors show up in most programmers' code from time to time, but I see them consistently in new programmers' code. Be aware of them in order to avoid them and polish your code in the process.
A public class name doesn't match the filename
Each .java file may contain only a single public class. That public class's name must exactly match the filename before the .java extension, including matching the case. For instance, a public class named Myclass must be in a file named Myclass.java - not in myclass.java. This is a syntax error.
A class is not in the correct directory
This syntax error occurs if the javac command cannot find a .java file in the expected directory. If a class is in the default package (it doesn't have a package declaration on its first line), then it belongs in the current working directory or in the directory that the sourcepath javac flag points to. If a class is in a named package, then it belongs in a subdirectory under the current working directory or in a subdirectory under the directory that the sourcepath javac flag points to. For example, if there is a class called com.hello.HelloWorld then its name is HelloWorld and its package is com.hello. For example, if your sourcepath flag is set to c:/src, then the HelloWorld.java file must exist in the c:/src/com/hello subdirectory.
Using equals versus assignment (== versus =)
In Java this error can be either a logic or syntax error. To compare two references for equality, you use the == operator (the equality operator). To assign a right-hand value to a left-hand variable, you use the = operator (the assignment operator). Newer programmers sometimes try code similar to:
if(myValue = expectedValue)
This code attempts to evaluate expectedValue as a boolean instead of the intended evaluation of myValue equaling expectedValue. If expectedValue is a boolean type, the code will have a logic error and will test whether expectedValue is true or false. If expectedValue is not a boolean type, the code will throw a compile error because the if statement requires a boolean value to be returned by the comparison statement, (myValue = expectedValue), but in Java the = operation always returns the right-hand value. When I read code I always pronounce the = operation as "gets assigned", e.g. "a = b;" would be read as "a gets assigned b."
Capitalization errors
Java is case sensitive! Referring to an identifier using the wrong case will cause a syntax error.
Forgetting that Java is zero-indexed
Java arrays and Lists use zero-based indices. The first element of an array or List is at index zero, e.g. myArray[0] or myList.get(0). Make sure your for loop iterations do not cause off-by-one errors. If you make one too many loops, you will get an ArrayIndexOutOfBounds exception. If you make too few loops, you may have a logic error.
NullPointerException
The NullPointerException; the bane of Java programmers. NullPointerExceptions are logic errors caused when a program attempts to access a method or attribute on a reference variable that is null.
If your object has not been initialized or has been set to null via the = operator, then calling a method on it or accessing one of its attributes is not valid. This often happens when an object is initialized with the return value of a method call. If that method call returns null - and this is common in APIs - then your reference variable will be set to point to null instead of to a valid object as you expected.
I hope this short list of common coding errors will start you thinking defensively while writing code. The more common errors you avoid outright, the more productive you will be overall. Your code will be have more polish and you will have more fun.
Tuesday, October 9, 2007
Common Java Coding Errors
Posted by Gaurav Shukla at 3:00 AM
Labels: cell phone operating system, j2me, java coding, java me, tip, tips and tricks
Subscribe to:
Post Comments (Atom)

0 comments:
Post a Comment