What is the use of the finally block? Is finally block in Java guaranteed to be called? When finally block is NOT called?

Finally is the block of code that executes always. The code in finally block will execute even if an exception is occurred. Finally block is NOT called in following conditions If the JVM exits while the try or catch code is being executed, then the finally block may not execute. This may happen due to … Read more

What is OutOfMemoryError in java? How to deal with java.lang.OutOfMemeryError error?

This Error is thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector. Note: Its an Error (extends java.lang.Error) not Exception. Two important types of OutOfMemoryError are often encountered lang.OutOfMemoryError: Java heap space The quick solution is to add … Read more

Is Java Pass by Reference or Pass by Value?

The Java Spec says that everything in Java is pass-by-value. There is no such thing as “pass-by-reference” in Java. The difficult thing can be to understand that Java passes “objects as references” passed by value. In Java, the argument passing mechanism is often a source of confusion. Java is always “pass-by-value,” but it’s essential to … Read more

Why String class is final or immutable?

It is very useful to have strings implemented as final or immutable objects. Below are some advantages of String Immutability in Java Immutable objects are thread-safe. Two threads can both work on an immutable object at the same time without any possibility of conflict. Security: the system can pass on sensitive bits of read-only information … Read more

What is difference between String, StringBuffer and StringBuilder? When to use them?

The main difference between the three most commonly used String classes as follows. StringBuffer and StringBuilder objects are mutable whereas String class objects are immutable. StringBuffer class implementation is synchronized while StringBuilder class is not synchronized. Concatenation operator “+” is internally implemented by Java using either StringBuffer or StringBuilder. Criteria to choose among String, StringBuffer … Read more