Common Mistakes made by a Java Programmer
It’s natural to make mistake, even if you are new to java or an expert in coding. Java errors can be either syntax errors or they can be logical errors. Syntax errors are also known as compile time errors and are illegal uses of terms and statements that are used in programming language rules. When a compiler can catch these errors, it then generates an error message that indicates the name of the class, line number of the java error and the type of java error. Sometimes the actual java error is not actually in the line that is included in the message.
Here’s a list of common mistakes done while coding in java :
- The most common Java error made by programmers is null pointers. A compiler will be unable to find this problem, which means it will only be discovered when the program is in runtime. This error occurs when the program attempts to access an object and the reference to that object is null. This will cause a NullPointerExecption that will cause a java error when the application is running.
- Overriding is very helpful feature and is heavily used in java. Easiest way of trap is mistype of the method name. If we mistype the name, we are no longer overriding the method but creating a new method but with the same parameter and return type. Compiler doesn’t not pick up this kind of problem and it can lead to frustrating results.
- When we use the == operator, we are actually comparing two object references, to see if they point to the same object. We cannot compare, for example, two strings for equality, using the == operator. We must instead use the .equals method, which is a method inherited by all classes from java.lang.Object.
- Java uses both way of passing by value and passing by reference, so we need to understand which one to use when. When we pass a primitive data type, like char, int, double, float to a function then it is called passing by value. It means a copy of that data type is duplicated and passed to the function. If the function chooses to modify the value, the copy value is modified. No change to the real variable is done. When we pass Java objects like array, vector or String to a function then it is called passing by reference. So when we pass an object to a function, we actually pass a reference to it not a duplicate. Any change is permanent to the member variables.
- Another kind of java error that occurs is when java cannot find the class. Be sure to save your class or source codes in the same directory where your javac.exe file is located. This file is responsible for compiling your codes and is also created automatically when a programmer downloads and install the DSK machine. Programmers should remember also that javac is not a recognized internal or external command and is not an operable program or batch file.