10 points about wait(), notify() and notifyAll() in Java?

If you ask me one concept in Java which is so obvious yet most misunderstood, I would say the wait(), notify() and notifyAll() methods. They are quite obvious because they are the one of the three methods of total 9 methods from java.lang.Object but if you ask when to use the wait(), notify() and notfiyAll() in Java, not many Java developer can answer with surety. The number will go down dramatically if you ask them to solve the producer-consumer problem using wait() and notify(). Many will use if block instead of while loop, many others will get confused on which object they should call wait() and notify()method? Some of them even succeed in creating livelock, deadlock, and other multithreading issues.

Difference between Static vs non Static method in Java

One of the key difference between a static and a non-static method is that static method belongs to a class while non-static method belongs to the instance. This means you can call a static method without creating any instance of the class by just using the name of the class e.g. Math.random() for creating random numbers in Java. Often utility methods which don't use the member variables of the class are declared static. On the other hand, you need an instance of the class to call a non-static method in Java. You cannot call it without creating an object because they are dependent upon the member variables which has different values for different instances.  One more important difference between the static and non-static method is that you cannot use a non-static member variable inside a static method, you cannot even call a non-static method from the static method, but the opposite is true e.g. you can call a static function from a non-static method in Java.

Double Checked Locking in Java and Why it was broken before JDK 5?

Double checked locking pattern is one of the interesting topics on Java Interviews. Earlier, it was asked to see if Java developer can write code using synchronized block or not and now it ask to gauge the candidate's understanding of concurrency, volatile and synchronization in Java. One of the simplest ways to write thread-safe Singleton was to make the getInstance() method synchronized but prior to JDK 1.6, a simple uncontented synchronization block was expensive and that lead many developers to write the getInstance() method of Singleton class using double-checked locking idiom. This was one of the clever idiom of that time which only uses synchronization when the Singleton object is created as seen in the following code and thus improves the performance of getInstance() method, which is used to retrieve the Singleton object.

Difference between StringIndexOutOfBoundsException and ArrayIndexOutOfBoundsException in Java

Though both StringIndexOutOfBoundsException and ArrayIndexOutOfBoundsException are children of IndexOfBoundsException class, former is thrown by methods of String and related class (e.g. StringBuffer and StringBuilder) to indicate that an index is either negative or greater than the size of the String, or more general not valid. For example, charAt() method throws StringIndexOutOfBoundsException when an index is equal to the length of String? why? because String is backed by character array where index starts at 0 and ends at length -1. The ArrayIndexOutOfBoundsException is  also quite similar to former but thrown by code which deals with array access to indicate that array is accessed by invalid index. The index is either negative or greater than or equal to the length of the array.

How to Convert String to LocalDateTime in Java 8 - Example Tutorial

Suppose you have a date time String "2016-03-04: 11:01:20" and you want to convert this into a LocalDateTime object of Java 8 new date and time API, how do you do that? Well, if you have worked previously with String and Date then you know that you can parse String to Date in Java. Prior to Java 8, we used to use SimpleDateFormat, which was mutable and not thread-safe, and advised not to be shared between threads. In Java 8, you can use the DateTimeFormatter class to convert String to LocalDate, LocalTime or LocalDateTime. Unlike SimpleDateFormat, the DateTimeFormatter is immutable and thread-safe and you can use it as a static variable for sharing and reuse. It also comes with pre-defined date time format e.g. ISO-8601 format. The ISO_LOCAL_DATE_TIME formats or parses a date-time without an offset, such as '2016-03-04T10:15:30'.