Most common mistakes made by beginners in Java programming

Most common mistakes made by beginners in Java programming

Which is the most common mistake every beginner makes while coding in Java programming language? There are some basic coding mistakes that people make again and again. Here is a list of some of the most common mistakes made by beginners in Java programming language. Knowing them will help you to improve your coding skills.

Confusing the assignment operator with the comparison operator (using  ‘=’ rather than ‘==’ )

That is the most common error beginners make. For example writing, if(a=b). This will result in a compile time error.

Using == instead of .equals() to compare strings

When we use the == operator, we are actually comparing two object references. We must use the .equals() method which is overridden in String class to compare contents(character sequence) of two strings.

null pointers

This is one of the most common errors that Java programmers make. Compiler will not tell you the possibility of null pointer exception because it is thrown at run time(sometimes a warning may be shown in eclipse depending on the code).

When you are trying to access an object, and the reference to that object is null, a NullPointerException will be thrown. Generally the cause of null pointers is that either you haven’t initialized an object, or you haven’t checked the return value of a function.

The best practice is to include conditional null check if you are unsure of the value of an object at a certain line of code.

Missing the ‘break’ Keyword in a Switch-Case

We often forget to write break statement in each switch case. If you don’t put a “break”, the control flow may go through the entire switch statement until it reaches a break or end of switch statement.

Not closing IO connections

Most often, while working with IO operations like File read or write, we don’t close the stream. For example,

Scanner in = new Scanner(System.in);
System.out.println(“Enter a number: “);
int n = in.nextInt();
System.out.println(“Entered number is: “+ n);

In the above code we forget to write in.close(); In this case, if any exception occurs, then the input stream remains open(Resource leak).

Using Raw Type Instead of a Parameterized One

While working with collections, even if we know the type of object that will be added to the collection, we forget to parameterize it during creation. For examples,

List nameList = new ArrayList();

In this case, though we know that we will be adding string objects to the list, we use raw type List instead of parameterized type List<String>(you can see a warning in eclipse “References to generic type List<E> should be parameterized“).

Not overriding equals() and hashcode() method

While using user defined objects with collections that that rely on hashing(like HashSet, HashMap etc.), we often forget to override the equals and hashcode method. When you don’t override these methods, then the underlying collection may not work as expected. For example, HashSet may contain duplicate (employee)objects. Hence when using custom objects (like employee objects) with hash based collections, it is necessary to override equals and hashcode method and also to maintain the contract between them.

Accessing static methods with object reference or this

We should remember that static methods are not bound to any object and should be accessed using the class name. Though it will not cause any issue when accessed using object reference, it is a good practice to access static methods using class names.

Trying to access non-static member variables from static context (static methods or static blocks)

When you are new to Java, you will have problems with accessing member variables from main method. You should note that main is a static method and you cannot directly access member variables. Member variables are bound to a particular object and you must create an instance of the object to access non-static variables(instance variables) inside a static method like main.

Forgetting that Java uses zero based index and ending up with ArrayOutOfBoundsException

In Java, arrays are zero-indexed, meaning that the first element’s index is 0 and last element is at index length-1. So when you are using a for loop to iterate an array you should setup the index to range from 0 to length-1. But we forget this and most often write like,

for(int i=1; i<=arr.length();i++) // throws exception when i equals to array length.

Not knowing that Java uses passing by reference for objects

When you pass a primitive data type, such as a char, int, float, or double, to a function then you are using pass by value. It means that a copy of the value is passed to the function. If the function modifies it, it will be modifying the copy only and the original value remains unchanged.

When you pass a Java object, such as an array or a list, to a function then you are using pass by reference. It means that a reference to the original object is passed to the function. If the function modifies it, the changes will be reflected in the original object as well. You should be aware of this when you are passing an object to a function.

On a side note, since String objects are immutable, it will be passed by value.

Not using iterator to remove elements from a collection

Iterator should be used if you want to remove elements from an underlying collection. If you use other means of removal, it may not work properly. For example,

The above code will throw ConcurrentModificationException. Below is the correct way of removing an element from a list.

Giving improper access level to member variables

While designing a class, it is a good practice to declare the member variables as private and provide public getter methods. But most often developers use public access or does not provide any access level(default access). This is a bad design and thumb rule is to give access level for instance members as low as possible.

Not naming the variables properly

A simple and most common mistake I see with most beginners is the use of improper variable names. Using ‘i’ , ‘j’, ‘k’, ‘s’, temp etc as the variable names makes it harder to understand the code. Always name the variables and methods meaningfully.

That’s all about some of the common errors made by beginners in Java programming. If you are aware of any other common coding problem in Java, kindly mention in comments section.

“Learn from the mistakes of others. You can never live long enough to make them all yourself”.

~Eleanor Roosevelt

The following two tabs change content below.
Working as a Java developer since 2010. Passionate about programming in Java. I am a part time blogger.
One comment
  1. It’s natural to make mistake, even if you are new to java or an expert in coding. there are some common mistakes. Overriding is very helpful feature and is heavily used in java. Easiest way of trap is mistype of the method name.

Add Comment

Required fields are marked *. Your email address will not be published.