Top 5 Mistakes Most Java Programmers Make

November 12th, 2012 Leave a comment 5 comments
Like the article?
Top 5 Mistakes Most Java Programmers Make

Everyone makes mistakes. Even the best programmer will have errors in their code every now and then, and no matter whether you are a beginner or a master there are always going to be some frustrating errors or mistakes that you make that will come back to haunt you days, weeks, or months later in the production cycle. Don’t let programming mistakes come back to bite you down the line – take a look at some of the most common mistakes even experienced Java programmers make in their programs and remember to think of them next time you are faced with a thorny issue that just doesn’t seem to go away!

5. Mistyped Override Function Name

This is by far one of the most common and most frustrating errors that happens now and again. Take a look at the following code block:

public class MyWindowListener extends WindowAdapter {
        
        public void windowDeactivate(WindowEvent e) {
               System.exit(0);
        }
});

That code will compile just fine. However, absolutely nothing will happen when the user minimizes the window, because that should be “windowDeactivated” instead of “windowDeactivate”. If the function is misspelled, the compiler will have no idea and won’t catch it, and you’ll be left trying to figure out why your code block isn’t working. Better yet, often the typo is hard to spot, and you’ll be left scratching your head, sure that “windowDeactivated” is running (and it is, your code just doesn’t override it!). If you are having problems with overrides, the first thing you should do is check and double-check your function names – chances are there is a missing or added letter somewhere that shouldn’t be!

4. Passing By Value vs. Passing By Reference

This is another sticky one to spot, especially because it’s often the case that it’s not entirely clear as to when exactly you’re passing by value and when you’re passing by reference and why that matters.

Passing by value is when you use a primitive data type like an int, float, char, or double and pass it to a function. When this happens, a copy of that data type is sent to the function instead of the actual data itself. When the function performs any modification to the value, only the copy is being touched: unless the new value is put as a return value, nothing will have happened to the original data.

When you pass an object like a vector, string, or array, however, you are passing by reference- that means you are actually passing the data type to the function, and any modifications made to the object are permanent even without the use of a return statement since the function is using the original data.

Quite often these two differences cause strange problems when you think you’re dealing with one and you are in fact dealing with the other – if you are seeing strange value changing errors, take a look at your functions: chances are something’s passing differently than you’d expect it to!

3. Not Writing Exception Handlers

This isn’t technically an error, but it’s most certainly a very bad programming practice that can lead to mistakes that can be harder to spot. Don’t write blank exception handlers! If you don’t have any error messages, mistakes can’t be caught easily and you’ll have no idea what’s going on. Even if it’s just a short, unique string that you can match against the specific handler, make sure you have some sort of error message string for your exception handlers so you can more easily and quickly find and catch bugs later. A second or two of work writing exception handlers can save minutes or hours later trying to find out a mistake later!

2. Comparing (== vs .equals)

Though this is more often a beginner’s mistake, it’s amazing how often it pops up in code from more experienced developers as well, especially from developers coming from different languages who aren’t entirely comfortable working in Java’s syntax yet. The == operator only compares two references- i.e. whether the two object references point to the same object. The equals function actually compares the two values for equality:

String string1 = "Hello World!"; 
String string2 = "Hello World!";
boolean doEqual = false;

if (string1 == string2) {
	doEqual = true;  
}

// vs.

if (string1.equals(string2)) {
	doEqual = true;
}

Those two strings will fail the == operator (doEqual = false), but will pass the .equals() method (doEqual = true) because they are equal in value but do not reference the same object. You can only compare them using the equals operator – don’t forget this when working and using Java to compare different values!

1. Capitalization Errors

This is the most common mistake that most Java programmers make, and it’s also the mistake that seems the most unlikely to make and the easiest to spot. Most experienced Java programmers reading this will swear up and down they would never mistakenly capitalize something or forget to capitalize something, but just as many of them will attest to the fact that they’ve done this a time or two. It’s sometimes extremely difficult to discover when you’ve done this, as our minds tend to read over capitalization errors, especially in long programs with long function names. If you’re finding strange errors regarding a function you know is there, always, always check the capitalization- it’s possible you forgot, and it’ll save hours of heartache looking for the problem!

Conclusion

Everyone makes mistakes, and every programmer will likely make all of these more than once. The trick, however, is to recognize that these are common problems and to look for them when you get misbehaving code- it’ll help in the long run to have a bit of an educated guess as to where the problem in your code might lie!

Help us spread the word!
  • Twitter
  • Facebook
  • LinkedIn
  • Pinterest
  • Delicious
  • DZone
  • Reddit
  • Sphinn
  • StumbleUpon
  • Google Plus
  • RSS
  • Email
  • Print
If you liked this article, consider enrolling in one of these related courses:
Don't miss another post! Receive updates via email!

5 comments

  1. Frisian says:

    #5 and #1: That’s what @Override is for.
    #4: Only surprising, if you don’t know the difference between an object and a reference pointing to it. Following a consistent design, the reference is passed by value

  2. Dan says:

    Interesting post. Just one correction: Java always uses pass by value, never pass by reference.

  3. Interesting, but I feel the need to point out the typos in your examples detract from some of the points you are making!

    Your #5 illustrates your #1 mistake. Functions following Sun’s coding standards would never begin capitalized.

    In #2 I’m pretty sure you meant to show String assignments (=) in your example code. However, the trivial example you use actually illustrates a case where == works, because of String interning. Under rare circumstances == can be useful over .equals () for up to a 5x performance benefit.

  4. Thanks for fixing it up! That #2 is a toughie though, have you tried throwing your example in a main () method and actually running it? I think you’ll find doEqual is true in both cases. :-P

    Of course it’s only because writing an example to prove a point can be tricky when you’re trying to avoid features like interned strings. Try this:

    class Test1 {
    public static void main (String[] args)
    {
    char c = ‘a’;
    String s1 = “string1″+c;
    String s2 = “string1″+c;
    System.out.println (s1==s2);
    }
    }

    Adding the ‘c’ variable makes it so the compiler can no longer actually assign the strings to the same location. Without it the output is still ‘true’.

  5. Ron says:

    Okay, as people pointed there are quite a few errors in your examples (though the general idea is correct).

    Since Java 6, you can’t override a method without annotating it with @Override. If you don’t use the annotation, and override a method, you will get a compiler error. If you use the annotation but in fact that method does not override anything, you will get a compiler error.

    Java is pass by value. Always. For further information, please read http://www.yoda.arachsys.com/java/passing.html or just search the internet a bit.

    Using String equation for the example may have not been the best choice due to interning. For more information – http://javatechniques.com/blog/string-equality-and-interning/ . The misuse of == vs. .equals() is a valid point nonetheless.

Comment