Build Tools (Ant, Maven)

  • In this post I’ll discuss why build tools are required and how they really work in top level.
  • For a specific build tool it expects the project to be in specific structure. E.g: web.xml and lib folder should be present only in WEB-INF folder. Then only it’ll be able to build and deploy. Like that each build tool have their own standards of default project structure. Another example is Maven expects pom.xml to be present in the project in the root directory.

Why Ant or Maven scripts are required?

  • So far we discussed that each build tool expects project to be in specific structure then only it can be able to build and deploy. If your project is not in specific structure which the build tool is expecting, then you can write a script (Ant script, Maven script) by giving the paths of required folders in the script, which makes the script to build and deploy the project.
  • Another reason for writing scripts is that, if you want to deploy the project with out manual process, you can write the script and prepare a batch file to do that job.

Sample Ant script:

you can find sample ANT script from below website:

Singleton Implementation with Double Checked Locking in Java

-> Singleton implementation is nothing but creating the object instance only once and reusing the same instance when ever needed, instead creating a new one each time when needed.
-> In the below code instance is of lazy loading initializing type as the instance of class is declared in static variable but which is initialized only when the method getInstacne is invoked. Eager loading will be like declaring and initializing at the same shown below.
private volatile static Singleton instance = new Singleton();

Double checked locking Idiom:

-> Now lets talk about Double checked locking idiom in singleton implementation. Here object is null or not will be checked twice and then object for the class will be created.
-> Why Volatile is used?: In double checked locking idiom, suppose if thread A is entered first null condition and then second null condition, then it will create an object for the class. Now suppose if thread B is entered first null condition, at the same time for thread A object is getting created (but instance creation is not completed yet) then as the null condition fails it’ll just return the same object instance which not completely initialized (half baked object), which leads to unexpected behavior of object.
So when voltaile is used, which makes the object getting published only when it is completely initialized, which avoids above problem.
-> The real problem is that Thread A may assign a memory space for instance before it is finished constructing instance. Thread B will see that assignment and try to use it. This results in Thread B failing because it is using a partially constructed version of instance.


public class Singleton {
private volatile static Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
return instance;



Data Types in Java

Primitive Data types:

In Java there are eight (8) primitive data types. For all those primitive data types wrapper classes also available.

If you define the primitive data types as global variables they hold some default values which are given below.

/* Primitive data types and their default values*/
static byte by; //0
static char c; // space
static short sh; //0
static int i; //0
static long l; //0
static float f; //0.0
static double d; //0.0
static boolean b; //false

Note: If you define the primitive data types as local variables they hold nothing and you must initialize them.

Wrapper Classes (Non Primitive Data types):

If you define the wrapper class data types as global variables then they hold some default values which is null

 /* Wrapper Class data types */
static Byte by1; //null
static Character c1; //null
static Short sh1; //null
static Integer i1; //null
static Long l1; //null
static Float f1; //null
static Double d1; //null
static Boolean b1; //null

static BigDecimal bd; //null
static String s; //null

Note: If you define these wrapper class data types as local variables they hold nothing and you must initialize them.  There are few more wrapper classes like Array, List etc.

Eclipse Shortcuts:

The post discuss about keyboard shortcuts for various function in Eclipse IDE which are very useful for rapid development.

Searching a file with its name:  Ctrl + Shift + R

Search a text in all the files: Ctrl + h (File Search tab)

Run java file: [Alt + Shift + X    & J] ,    [Ctrl + F11]

Navigate to the selected text in other places of file:   Ctrl + K

Organize Imports: Ctrl + Shift + O

To Navigate to the start/end of the block:  Ctrl + Shift+ P

To see the list of opened files and to navigate to them:  Ctrl + E

Suggestion when you are typing: Ctrl + Space

To get the list of methods which you can override:  [Alt +  Shift +  S     &  V]

To get the System.out.println();   [syso & Ctrl + Space]

To clear the breakpoints: Run-> Remove all breakpoints/ skip all breakpoints [this option will be shown only when application is running in debug mode]

To open IDE views (like console, variables) : Window -> show view

To know the result of a method: Ctrl+i




Sorting in Java

Sorting the numbers is easy in Java. Collections utility class provides the ‘sort’ method for sorting the numbers. [Don’t confuse with Collection which is an Interface]. To the Collections  Sort method you need to input numbers in List format. Though numbers present in Array also can be converted to List and then can be sorted. Java also provides another utility class ‘Arrays’ which has also Sort method. You can use either class methods for sorting on your convenience.

Collections.sort method expects List as input [array can also be send by converting to list object using Arrays.asList(arrayObj) method]

Arrays.sort method expects Array as input [List can also be send by converting to Array object using listObj.toArray() method]

Eg1: Sorting Numbers in List

List <Integer> numLst1 = new ArrayList<>();
int f = 0;
while(f< numLst1.size()){

o/p:   [4    10     20]

Eg2: Sorting Numbers in Array

Here we are converting Array to List and then sorting.

Integer[] numLst1 = {10, 30, 05};
int f = 0;

o/p: [5    10     30]

Eg3: Sorting numbers and finding smallest number

Now let’s get into some deep by taking a requirement which returns the smallest number from the Array which should not be 0. Means if the Array contains numbers like [12, 20, 5, 30, 0, 50] then it should return smallest number as ‘5’ (not zero).

Integer[] returnVal = {10, 5, 20, 0};
int f= 0;
int smallValue = 0;
while(f < returnVal.length){
if (returnVal[f].intValue() !=0){
smallValue = returnVal[f].intValue();

o/p:   5

Sorting Numbers in HashSet & HashMap [not with the help of Sort method]

This can be achieved with the help of TreeSet. From TreeSet if you try to move to HashSet again then the insertion order will be lost. So keep the sorted result in TreeMap only or store them into LinkedHashSet where the insertion order is maintained. Similarly for HashMap sorting can be achieved with the help of TreeMap.

HashSet<Integer> hs = new HashSet<>();
TreeSet<Integer> ts = new TreeSet<>();

o/p:     hs:[20, 6, 10, 12]      ts:[6, 10, 12, 20]

equals() vs contentEquals() vs ==

Why equals method is used?

To compare two string contents [One string can be in stack memory and other can be in Heap memory, still it compares the contents of the strings]

String s1 = “abc”;

String s2 =  “abc”;

String s3 = new String (“abc”);

System.out.println(s1.equals(s2));  // o/p: True  [hashcodes are same in this case]

System.out.println(s1.equals(s3));   // o/p: True [hashcodes are different in this case]

Why ‘==’ is used?

It is an operator which compares the Hashcodes (not the content present inside them).

String s1 = “abc”;

String s2 =  “abc”;

String s3 = new String (“abc”);

System.out.println(s1==s2);  // o/p: True  [hashcodes are same in this case]

System.out.println(s1==s3);   // o/p: False [hashcodes are different in this case]

Why contentEquals is used?

It is used to compare the String object with StringBuffer/StringBuilder/ Char Array objects.


  • You can’t compare String object with StringBuffer object using equals method.
  • You can compare String object with another String object using equals and contentEquals methods. [both are same, but equals is specific to compare strings only]


String s1 =”abc”;

String s2 =new StringBuilder(“abc”);

System.out.println(s1.contentEquals(s2)); // o/p: true [hashcodes are different in this case]


String Is Immutable?


Yes, String is Immutable. Means once the data is assigned to a String variable, it stores in a memory slot where the data can not be modified. If you try to modify the data, it’ll create a new data slot and the reference variable point to the new dataslot.



String s = "abc"; //This will create a dataslot in string pool of stack memory, and point the reference variable 's' to that data slot.



// Guess the output? It is "abc". [the concated data is stored in a new dataslot, but reference variable 's' is still referencing to the old dataslot]

s = s.concat("def");


// Now the output is "abcdef". [The concated data is stored in new dataslot and the reference variable 's' is pointing to this new dataslot];


Then, the data present in previous dataslot of string pool won’t be changed, instead a new dataslot will be created in the string pool and the reference variable ‘s’ will point to the new dataslot created.

Then what about the previous dataslot?

Garbage Collector will take care of that. GC will clear the memory slots of all in regular cycles, to those there are no reference variables. So that memory will be cleared.

So with the above example and discussion, we concluded that String is Immutable.

StringBuffer & StringBuilder:

Before discussing about StringBuilder& StringBuffer, first let’s know why these are used when we have ‘String’. The answer is because String is Immutable and these are Mutable versions of String. Means when you have a requirement that you need to add/modify the data multiple times after initial assignment, then use StringBuilder. If you do the same with String, it’ll create a new memory slot for data for each modification which leads to memory leakage/wastage.

StringBuilder & StringBuffer are mutable. Means once the data is stored in a dataslot, if you modify the data, then it won’t create a new dataslot, it just updates the existing dataslot and the reference variable point to the same dataslot.

StringBuffer sb = new StringBuffer("abc"); // mutable, synchronized

StringBuilder sbuilder = new StringBuilder("abc"); // mutable, non synchronized


Note:  StringBuilder is introduced in Java 1.5, in Java 1.4 there is only StringBuffer.

Why StringBuilder came when there is StringBuffer?

It’s very rare that you really need to build strings in a thread-safe manner, so why pay the synchronization cost? So that is the reason StringBuffer is obsolete now.