20 likes | 127 Views
There are some basic coding mistakes that people make again and again. Here is a list of some of the most common mistakes made while answering Java interview questions.
E N D
What are common errors made while answering Java Interview questions? There are some basic coding mistakes that people make again and again. Here is a list of some of the most common mistakes made while answering Java interview questions. 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. 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 example, List nameList = new ArrayList(); 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.