5 Common Java Mistakes

 

 

Every programmer has to debug code. Itís a fact of life. Some programmers, however, have it much easier than others because they either have more debugging experience, or know what errors are most common and therefore immediately look for them. The following list is my opinion of the most common mistakes, with #1 being the most common.

 

#1. Capitalization and Spelling

 

Java is a case sensitive language. Not only does everything have to be spelled EXACTLY right, but the capitalization also has to be correct. For example, some people might put the following into their code:

 

button main=new button();

 

††††† You simply can't do this. You will get an error if you don't capitalize the letter B in button (both times). Luckily for us, most class names has the first letter capitalized, and that is it. For example, List, Choice, Button, and Label, are classes in the AWT. The first letters are capitalized. If you have a problem with capitalization, you should probably try to notice capitalization patterns. Spelling is something you just have to memorize.

 

#2. Forgetting ;

 

Every programmer is guilty of forgetting a semicolon once in a while. Semicolons are used to end Java statements. Luckily, compilers are pretty good when it comes to spotting missing semicolons, and that makes life a lot easier.

 

#3.Missing Braces and Parentheses

 

() and {} are used a lot in Java. Braces are used make a group of statements into a block of code. Every applet has braces and parentheses. For each opening brace or parenthese, there should be a closing one. A missing brace especially will pop up a huge list of errors when you try to compile.

 

One trick to avoid forgetting a parenthese or brace is to create both the opening AND closing ones and THEN fill in the code in between. For example, whenever I am writing an applet, I create the opening and closing braces which will contain the code for the applet, and THEN I fill in the code. This usually takes care of the problem.

 

#4 Forgetting to new

Though pointers no longer exist in Java, you still need to initialize their replacement: references.A reference is somewhat like a pointer, except that it has automatic garbage collection.But, as you work with items and objects in Java, you will often still need to initialize the reference, much like you would initialize a pointer.For example:

 

Button main=new Button();

 

Here, main is a button-type variable, that is initliazed with a reference to a new Button object.If we forgot this step, and then tried to access a member function of main, it would generate an NULL pointer exception.

 

 

#5. = vs. ==

 

One very common mistake that people make is they use a = when they should use ==, or vice versa. = and == are two completely different operators.

 

For example, if I wanted to make p have the same value as q, I would write the following line:

p=q;

 

 

The line above says, assign the value of q to p. == is used to test if the value of something is ALREADY equal to the value of something else. If there was an if statement that used = instead of ==, it would have a major problem. Instead of checking if they are equal, it would assign the value of the number or variable after the = to the variable before the =.

 

Here is a rule of thumb:. Only use == in loops or conditional statements in between the parentheses. All other times, use =. This works really well. There might be some cases where it doesn't hold true, but it will help.