5 Common Java Mistakes That You Can Make
If you’re a professional Java Developer, they you probably already know what are the most common mistakes in Java that a person can make. But if you’re a beginner Java Developer or you’re still learning the Java language, then you might want to know what are the most common Java mistakes that you can make so you won’t make them in the future.
Common Java mistake #1 – Comparing Java with other programming language
One of the most common mistakes that most beginner Java Developers make is comparing Java to another Object Oriented Programming language or just another programming language. This is especially true if you already know some other OOP language like C++ or .NET or you can program with dynamic programming language like PHP or Perl.
But probably the biggest mistakes of them all is to compare Java to a procedural language like C. You have to know that Java is the opposite of procedural languages and it can do many other things that procedural languages can’t.
So don’t treat Java like any other language, it’s an object oriented language that has its own rules, so you should learn to use it properly.
Common Java mistake #2 – Not using libraries
It’s no secret that even the bit more experienced Java Developers tend to ignore the libraries written in Java. One good practice is, before starting to code and imagining roughly how your program should look like, to search through the available libraries. You should also know that almost all of the Java libraries are free to use and many of them are constantly updated.
And there are so many Java libraries to choose from. You have libraries like Guava and Joda that have become a standard libraries that you can use in almost any project. You also have libraries like Struts 2 and Tapestry that are great for creating web applications. There are also libraries like Spring Data and Hibernate that are database related and they’re great for doing backend work etc.
Another great thing about using popular libraries is that they have a big community behind them, so if there’s an error in it, it’ll probably be quickly fixed and if you have problem using it you’ll easily find the solution.
Common Java mistake #3 – Confusing == and .equals( )
This is probably one of the the easiest most common java mistakes to make especially if you’re new to Java and you don’t know the different operators and functions that well. One thing that you need to know is, that == operator and .equals( ) method are somewhat similar, but they’re not the same thing. And because of their similarity you can easily confuse one with the other. This is what you need to know:
The == operator (equal to) is used when you want to compare two object references to see if they’re pointing to the same object. So it basically compares the value of two objects references and determines if the two objects refer to the same String instance. It returns a boolean ‘true’ if they’re equal, if not it’ll return ‘false’.
The .equals( ) method will compare what it’s assigned to compare and nothing more. It’s also used when you want to compare two Strings if they’re equal or not, which can’t be done with the == operator.
Common Java mistake #4 – Making Capitalization errors
Making capitalization errors is probably one of the most common java mistakes that we as Java Developers make. The frustrating thing about this kind of error is that you can do it quite easily without really paying attention to it and then you can spend hours and hours trying to figure out why the program doesn’t work as intended.
There’s no specific rule on how you can successfully avoid this type of error but you can keep two things in mind: First, you need to know that in Java API all methods and member variables start with lowercase letters and second, you need to know that all methods and member variables usually use capitalization where a new word begins like getCount() or getInstance() etc.
Lastly, another thing that can help you to avoid this mistake is to use a good programming environment suitable for Java like Eclipse or NetBeans. They will usually show you where the error is and suggest you several solutions on how you can solve it.
Common Java mistake #5 – Null pointer exceptions
You’ll probably make this error when you need to write your code really fast and you don’t check your return values. Also this one is a really sneaky Java error because you can really see it until you run your code and try to test it. In many cases the code runs fine until you start entering values and parameters and then you get the Null Pointer Exception.
When you try to access an object, and the reference to that particular object is null, you’ll get the NullPointerExeption. You’ll probably get this exception is if you haven’t checked the return value of some function or if you haven’t initialized an object. Of course these are the two main causes but there are also many other causes.
The best way to avoid this is to check your code thoroughly and to test your program before you say it’s completely done.