10. Method Overriding – mismatch of the method name: Overriding is very helpful feature and is heavily used in java. Easiest way of trap is mistype of the method name. If we mistype the name, we are no longer overriding the method but creating a new method but with the same parameter and return type. Compiler doesn’t not pick up this kind of problem and it can lead to frustrating results.
9. Comparing two Objects (== and .equals): When we use the == operator, we are actually comparing two object references, to see if they point to the same object. We cannot compare, for example, two strings for equality, using the == operator. We must instead use the .equals method, which is a method inherited by all classes from java.lang.Object.
8. Accessing non-static member variables from main method or static method: Main method is marked as static that means we don’t need to create an instance of the class to invoke the main method. If we want to access its member variables from a non-static method (like main), we must create an instance of the object. Here’s a simple example of how to correctly write code to access non-static member variables, by first creating an instance of the object.
public class Demo
public String my_variable = “ABCD”;
public static void main (String args)
Demo demo1 = new Demo();
// Access member variable of demo
System.out.println (“This WILL NOT cause an error” +
// Access a non-static member from static method
System.out.println (“This WILL cause a compiler error” +
7. Passing by value or Passing by reference: Java uses both way of passing by value and passing by reference , so we need to understand which one to use when.
When we pass a primitive data type, like char, int, double, float to a function then it is called passing by value. It means a copy of that data type is duplicated and passed to the function. If the function chooses to modify the value, the copy value is modified. No change to the real variable is done.
When we pass Java objects like array, vector or String to a function then it is called passing by reference. So when we pass an object to a function, we actually pass a reference to it not a duplicate. Any change is permanent to the member variables.
6. Java is Zero-indexed: In Java, arrays are zero indexed which means first element’s index is 0.
//create an array of 3 strings
String strArr = new String ;
//first element of the array
strArr = “First String”;
//last element of the array
strArr = “Third and last String”;
//will give ArrayOutOfBoundsException
System.out.println(“String at last element” + strArr);
Zero indexing applies to arrays, Strings, Date and Calendar also.
5. Using = sign rather than == : This is a very common mistake to make, confusing which one to use for assigning and comparing. Even we are not able to make out this mistake, compiler can point out by giving the message like ‘cannot convert abc to Boolean’ etc.
4. NullPointerException : NullPointerException is not detected by the compiler but is sparked at runtime. They’re exceptions that occur when you try to use a reference that points to no location in memory (null) as though it were referencing an object. Calling a method on a null reference or trying to access a field of a null reference will trigger a NPE. These are the most common, but other ways are listed on the NullPointerException javadoc page.
3. Capitaliztion Error : One can see this kind of error and yet cannot make out where the mistake is by not spotting the lack of capitalization. There’s some simple rules to not make this mistake –
- all methods and member variables in the Java API begin with lowercase letters.
- all methods and member variables use capitalization where a new word begins e.g – getDoubleValue()
Following some pattern like this will reduce this kind of error.
2. No Exception Handler – Writing a blank exception handler and to ignore the message is very easy. But when we run into problem and earlier had not written any error message, it becomes almost impossible to detect that issue. Even the simplest excpetion handler can be of benefit by using Try-Catch block.
1. Preventing access to shared variables by thread : When two or more threads access the same data concurrently , there might be a situation when they will access or modify the same data at the same time.
Such problems are not just limited to multi-threaded applications or applets. If we write Java APIs, or JavaBeans, then our code may not be thread-safe. Even if we never write a single application that uses threads, people that use the code WILL. The simple way is to make the variable private and use synchronized accessor methods.
Preparing for OCAJP 7 Certification? Pass in 1st attempt through Whizlabs OCAJP 7 Training Course! Start with Free Trial!
- Preparation Guide for the Splunk Core Certified User Exam - December 16, 2020
- Top 25 Tableau Interview Questions for 2020 - October 15, 2020
- Best Way to Learn Java for Beginners - October 8, 2020
- 20 PostgreSQL Commands You Need to Learn - September 8, 2020
- Oracle Announces New Java OCP 11 Developer 1Z0-819 Exam - August 31, 2020