Sorting string array using SetTree

Code is straight forward


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

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

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”

Java Database Connectivity (JDBC) basics

Simple JDBC program demonstrating java connectivity with database.

The DriverManager will try to load as many drivers as it can find and then for any given connection request, it will ask each driver in turn to try to connect to the target URL.

When a Driver class is loaded, it should create an instance of itself and register it with the DriverManager. This means that a user can load and register a driver by calling


A Connection is like a session with database. Applications should not invoke SQL commands directly to change the connection’s configuration when there is a JDBC method available. By default a Connection object is in auto-commit mode, which means that it automatically commits changes after executing each statement. If auto-commit mode has been disabled, the method commit must be called explicitly in order to commit changes; otherwise, database changes will not be saved. Following code is used for getting connection.

Note:In JDBC 4.0, we no longer need to explicitly load JDBC drivers using Class.forName().

Statements are  used for executing a static SQL statement and returning the results it produces.

By default, only one ResultSet object per Statement object can be open at the same time. Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects. All execution methods in the Statement interface implicitly close a statment’s current ResultSet object if an open one exists.

ResultSet is a table of data representing a database result set.
A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set.
A default ResultSet object is not updatable and has a cursor that moves forward only. Thus, you can iterate through it only once and only from the first row to the last row. It is possible to produce ResultSet objects that are scrollable and/or updatable.