String with variable parameters

Using string with parameters is very handy in case of Constants.

Java class in focus is: java.text.MessageFormat


Output will be

Welcome Mr. Nikhil to
Welcome Mrs. Tejashree to

Installing VirtualBox guest additions on CentOS or RHEL

Process is very simple

  1. Login as root
  2. Install dependencies # yum install gcc make kernel-headers kernel-devel
  3. Reboot for loading new kernel info if required and to be on safer side
  4. Use gmake instated of make #export MAKE=’/usr/bin/gmake -i’
  5. Insert guest additions CD
  6. goto installer # cd /media/VBOXADDITIONS*
  7. #./
  8. Reboot and enjoy

Creating multiple jars from single pom file

In ant we can create any number of jars using <jar> tag, but when we are moving to maven from ant it we may face challenge. Old code may not be modular imposing use of single pom file. In this case maven-jar-plugin come to rescue.


I guess xml is self explanatory, if question put it in comments

Full working source code

Adding internationalization support to spring mvc with thymeleaf

For basic Thymeleaf configuration check Spring MVC with Thymeleaf

Thymeleaf uses Standerd messange resolver unless specified which on to use. We can also use Spring internationalization support which will be supported by default by Thymeleaf.


  1. Add messages_en file to src/main/resources/
  2. Add spring beans those will support internationalization.
  3. LocaleChangeInterceptor is handler mapping that need to supports the multiple languages. The “paramName” is the parameter value that’s used to set the locale.
  4. Use ?lang=fr to change language to French from default English.

Source: Source Code

Java Concurrency – Processes and Threads


Process is self contained environment meaning it got its own basic resources importantly own memory space. For communication between processes we need interprocess communication (sockets, pipes etc) provide by operating system.

Most implementations of JMV runs as single process. Java application can create multiple or additional processes by using java.lang.ProcessBuilder object.


Threads exists within process and every process has at least one thread. Thread share process’s resources that includes memory, open files etc; hence creating new thread requires few resources. Threads sometime called as lightweight process. Each thread is associated with instance of class Thread.

Their are two strategies of  Thread
1. Manual – Control thread creation and management by instantiating Thread each time application want to perform asynchronous task
2. Executors – Separate thread management from application by passing tasks to Executor.

Defining and starting a thread

1. Provide Runnable object – Implement Runnable interface which defines a single method run()
2. Subclass Thread class – Extends Thread class.

It will be wise to implement Runnable interface instated of extending Thread class. Coz we can extend other classes if required if we implement Runnable interface but we can not if we extend Thread class.

Thread.sleep(long miliSeconds) causes current thread to suspend execution for specific time. Sleep timings are not guarantied to be precise as it depends on facilities provided by underlaying operating system.

Good example of basic thread working

Ref: Java SE tutorials

Design Pattern – Singleton

Singleton is simple pattern which guaranty  only one instance of object is present. Singleton is term taken from mathematical concept “Singleton is a set with exactly one element”

Singleton is achieved in java by

  1. By making default constructor private – hence you can not initialised the class .
  2. Having static method that can return you instance – control over how to create object
  3. Having self instance private static and volatile – Thread safe unique instance.

Note: volatile from Java 5 means value of variable will not be tread-locally cached. Value will be updated directly in main memory. Variable will act like its synchronised on its self.

Below is sample for double check singleton:

Reference:  WikiHead First Design Patterns

StringBuffer vs StringBuilder

To check StringBuffer vs StringBuilder we must check AbstractStringBuffer first coz both StringBuffer and StringBuilder extends AbstractStringBuilder.


AbstractStringBuilder has ‘char value[]’ that is used for holding string in from of char sequence. ‘int count’ is used for storing number of characters stored.

Important methods in AbstractStringBuilder:

  1. capacity(): Capacity is amount of store available for newly inserted characters
  2.  insert(int, <T>): insert <T> into character sequence at int.
  3. reverse(): Reverse of the character sequence
  4. append(): Append to the character sequence
  5.  toString(): Only abstract method in AbstractStringBuilder
  6. getValue(): This is a final method that returns char value[];

In simple words all operations are done on array of characters which is present in AbstractStringBuilder.

Now StringBuffer vs StringBuilder

  1. StringBuffer and StringBuilder extends AbastractStringBuilder
  2. Both use AbastractStringBuilder for logical operation and has minimal code written in them
  3. Both implements toString() where they create new String and do not share char value[] with String.
  4. Both implements writeObject() &  readObject()

Now the major and important difference is all important methods in StringBuffer are synchronized. Due to non synchronized methonds StringBuilder performs better than StringBuffer.

String Basics

Strings: String is a class used for manipulating strings. String is character array and implemented in same way. String is object in java.

Creating String:
String can be created in multiple ways

  1. By using one of thirteen constructors provided by String class (or using new keyword)
  2. By using ‘=’ operator (or String literal)

Important difference between them is when we create String object using String literal JVM checks string pool if found string object will be returned else new object will be created; but in case of new JVM will create new object without checking in string pool.

String equality:
String equal method check each character in character array that is present inside String; where as ‘==’ checks object memory location.

String pool:
String pool is special memory location where JVM keeps all string objects

String and security:
If we keep sensitive data in String due to immutable nature we can’t erase that data. Someone who got access to java memory like access to heap dump can read that information.
We should keep password in char array and reset them once they are no longer needed.

Character encoding:
By default String will use same encoding as underlaying platform i.e. character encoding of server. This can cause issues if server is not supporting unicode encoding like UTF-8 or UTF-16. This can be changed by using system property “file.encoding”