Archive for category Core Java olmazsa olmazlar

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 System.exit() call.
  • if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues.
  • If a exception is thrown in finally block and not handled then remaining code in finally block may not be executed.
  • Advertisements

    Leave a comment

    Difference between StringBuffer, String and StringBuilder in Java

    In summary here are list of difference between StringBuffer, String and StringBuilder in Java :

    1) String is immutable while StringBuffer and StringBuilder is mutable object.
    2) StringBuffer is synchronized while StringBuilder is not which makes StringBuilder faster than StringBuffer.
    3) Concatenation operator “+” is internal implemented using either StringBuffer or StringBuilder.
    4) Use String if you require immutability, use Stringbuffer in java if you need mutable + thread-safety and use StringBuilder in Java if you require mutable + without thread-safety.

    That’s all on famous String vs StringBuffer or StringBuffer vs StringBuilder discussion. All these differences helps to avoid common coding mistake of using String in place of StringBuffer in many places. from Java 5 onwards either use + operator of StringBuilder for concatenating String in Java.

    Read more: http://javarevisited.blogspot.com/2011/07/string-vs-stringbuffer-vs-stringbuilder.html#ixzz2bqhRXcYi

    Leave a comment