Difference between final, finally and finalize


Every java interview might have this question asked for a beginner. Probably this is the most known question and well sort for answers. Though there are tons of other articles explaining the differences about these, I am planning to put those difference in a slightly different and more understandable. I am expecting that I will definitely reach my objective, while it’s the perception of the reader that is really important.

Continue reading

Posted in JAVA | Leave a comment

Abstraction and Encapsulation Demystified

Abstraction in OOPS is well articulated, and states clearly of hiding the unnecessary details from other Components. Abstraction and Encapsulation comes hand in hand and often lead to misconceptions. In this article I would love to clear out all the ambiguity you ever had in mind about Abstraction and Encapsulation and their implementation in Java.

Wikipedia Definitions:

“In computer science, abstraction is the process of separating ideas from specific instances of those ideas at work. Computational structures are defined by their meanings (semantics), while hiding away the details of how they work.”

Encapsulation is the packing of data and functions into a single component. The features of encapsulation are supported using classes in most object-oriented programming languages, although other alternatives also exist. It allows selective hiding of properties and methods in an object by building an impenetrable wall to protect the code from accidental corruption.”

In layman terms, abstraction means hiding, and encapsulation means packing / gluing things together.

Continue reading

Posted in JAVA, OOPS | Tagged | Leave a comment

Factories in Software Development

From our childhood, we hear a lot of things about “Factories”, some are good and most of them are bad. That too if you are coming from a country like India, you will hear more bad stories of the factories destroying the communities and health.

In my own experience, I have heard a lot of criticism on factories, like they spoil the environment, contaminate the water, air and soil and devastation of the crops. When compared to the opportunities these give to the jobless the devastation gains and protests start.

Luckily, in Software development the “Factories” are doing the quite opposite of the physical “Factories” around the globe. As I have said above the “Factories” we are talking about reduces the pollution, improves the quality and gives more opportunities. (Ok finally one thing in common).

Continue reading

Posted in Uncategorized | Tagged , | Leave a comment

Sorting Binary Array with O(n) complexity

Welcome back, today am going to discuss about a solution I came up when solving a sorting problem given by my colleague. The problem statement is very simple and straight “Sort an Array of Binary Digits, with O ( n ) complexity (without using extra memory)”. 

Continue reading

Posted in Uncategorized | Leave a comment

Novice Info: Uninstalling Software From Eclipse


Today, I am planning to present a small tip, which I wanted to share from a long time. Many of us use Eclipse for all our development needs. The main reason being, it is well organized and has the wonderful framework for adding any capabilities for building on more features to it. In my opinion the awesome feature of writing and adding Plugins and Features of the eclipse for its extended support is the main reason for its penetration into the developer market.

Installation of plugins is very simple and easy in eclipse, inherently like maven it does have a repository for all the plugins and features call the update site, and from there you should be able to install any new feature/capability to the eclipse is very easy. There are of course a number of ways, to install these new features in eclipse, and these are out of scope for the content in this blog. The same way we have many ways to uninstall the software in eclipse, and in this blog am going to present what I know and applied in the my development experience.

First of all, before going any step forward about the ways of uninstalling a software, let us try to understand the reasons of why we need to think about removing any capability from eclipse.

  • If your eclipse is having many capabilities, and you are not using most of them in day 2 day development
  • Unused software in the background is hitting the performance of the eclipse
  • Need of installing new feature, which will not be compatible with the existing features.

These were some of the reasons, for uninstalling software from eclipse. Now, without taking any more time of yours let us look about the ways of uninstalling a feature on the eclipse.

Using Install Software Option:

  • Open “Install New Software” dialog box, by navigating through Help –> Install New Software.


  • Click on the “already Installed?” link as show in the above picture.


  • Select the features to be removed, and select Uninstall button.
  • Once the features are uninstalled your Eclipse program will be restarted, for the next use.

Voila, you are done with the Uninstallation of the features in eclipse.

Note: Yes you can definitely remove software, by deleting the jars in plugins and features directory in “ECLIPSE_HOME/” folder. But I don’t advice this as, it might break some other dependent plugins. Do this at your own risk of corrupting the Development Environment.

Thanks for the patience, in reading the article, and hope this was useful and not a waste of your precious time. Do comment your opinions and suggestions on making these articles a value for the next readers.

Posted in Uncategorized | Leave a comment

Importance of Data Structures and Algorithms in Programming


Today, I had a discussion with my colleague (Raveendra Chikkala), about Data Structures. It was a really interesting and motivating discussion, and today I wanted to share it with all of you. I have never taken the complexities of the algorithms seriously till date in my day to day programming, though I cared a lot on the design patterns and worked hard on making the system loosely coupled and having high cohesion.

Gist of the discussion is as follows:

Me: Why do we need to think of Data Structures, what do you mean by Data Structures. I know we use the data structures to store the data, and faster retrieval.

Friend: Yes, Data Modeling and Retrieving, is all about data structures, but we need to consider the complexity of the programs we write in every day programming, if we just use the data structures, and not considering the complexity of your own code is wrong. Predefined Data Structures, follows a set of algorithms, so that the data retrieval and update functions will be of high performance. While, it is also our responsibility to have good performance of the code we write.

Below is the example, he gave to justify his statement.

For Example Consider two lists List1 having some elements and L2 is having some elements. Now try to find the intersection of these two lists.

My Solution:

   1: for(Object ob: list1){

   2:     if(list2.contains(ob)){

   3:         list3.add(ob);

   4:     }

   5: }

He calculated the complexity of my logic as Looping n times in List1 will have a complexity of “N”, and complexity of contains() method on List2 is “N”, then the complexity of the logic above becomes N*N = N2 , which means the complexity increases exponentially. Consider each List is having 1 Million objects then the complexity will become 1000 Billion calculations.

His Solution:

   1: Collections.sort(list1);

   2: Collections.sort(list2);

   3: if(list1.size() > list2.size){

   4:     for(Object ob:list1){

   5:         if(Collections.binarySearch(list2,ob)>=0){

   6:             list3.add(ob);

   7:         }

   8:     }

   9: } else {

  10:  for(Object ob:list2){

  11:        if(Collections.binarySearch(list1,ob)>=0){

  12:            list3.add(ob);

  13:        }

  14:    }

  15: }

The above algorithm will have the complexity as follows, as Collections.sort() uses merge sort technique, and its complexity is given as “nlogn since we are calling Collections.sort() method twice on 2 lists the complexity will be “2*nlogn . The looping will have the complexity of “n” and as the binary search is having the complexity of “log(n)”. Hence the complexity of the loop becomes “n*logn. So the total complexity of the system will be “2*nlogn +n*logn”. Which is a Linear complexity. Considering each List is having 1 Million objects then the complexity will become “(2*106*2.5)+(106*2.5) = 7.5” Million calculations.

So, his solution of getting an intersection of two lists is far far far…. better than my way. I was shocked to see this, and understood the importance of understanding the complexity of the algorithms we write in day to day programming matters more than we can ever think.

If you did started thinking like me, that whatever quick solves you did in day to day programming as dirty code, then I am sharing some of the references he pointed to me in references link. Please go and have a read of those. Otherwise, also thanks for you patience and interest in reading till now, and am sure one day or the other you will find it important and come back again.

In addition to this, I learnt the importance of Mathematics, and Progressions in programming, and am now confident of saying a good mathematician with a passion to program will become the “Real Developer of the World”. I did resolute to become more realistic developer, now its your turn.


Raveendra Profile : http://in.linkedin.com/pub/raveendra-ch/2b/795/b5a/ 

Bill Gates on Programming : http://programmersatwork.wordpress.com/bill-gates-1986/

Posted in Uncategorized | Leave a comment

Singleton Pattern

As a programmer you should have come across many design patterns, in this blog entry I would like to spend some time on Singleton Design pattern. Before, further discussion on Singleton pattern, let us try understanding what is Design Pattern, and why do we need to learn about these patterns.

A Design pattern, is a specification defined to solve a particular problem. In our day to day programming we will be trying to solve several problems, design patterns are those solutions which are adopted by many developers around the globe to solve those specific problems you face today. Now its time for you to adopt some of those in your code, instead of fighting out your own solution for the problems you have on your table.

Below are few Design patterns, which were mostly adopted by many developers across the globe, in their day to day development.

  • MVC
  • Singleton
  • Factory Object
  • Builder
  • DAO pattern

Let us try to see the problem statements, that can be solved using the Singleton pattern and then we can dig more deeper on to what is a Singleton and what ways we can define singletons.

Problem Statement 1: My JVM goes OOM, because an object with heavy foot print is created several times, by several threads.

Problem Statement 2: I want a shared object across several threads during manipulation, without passing the references of that object.

Problem Statement 3: I want to define a utility class, but don’t want to create all the methods of that utility class as static.

If you, are stuck with any of the problem statements given above, then its time for you to implement a Singleton pattern for those objects. Now, that we know when to use a Singleton pattern, lets see what are its qualities, and what it offers for the developers.

  • Singleton objects can be instantiated only once
  • At any point of time there will be one and only one object in the JVM
  • All the threads will share the same object.

Now that, you have seen what all situations we can use Singleton, and what are the properties of a Singleton, let me show you how can we implement a Singleton pattern. A Singleton can be implemented using several techniques, I will try to cover some of those in this post.

1. Eager Instantiation

2. Lazy Instantiation

a. Locking

b. Double Checked Locking

c. Double Checked locking using volatile keyword

c. Using Inner Class

3. Using Enums as Singletons

Each of the above techniques with implementation details and examples, are explained hereunder, for giving you basic view of each of the technique.

Eager Instantiation:

Eager instantiation means, the object is initialized when the class is loaded, even before there is any actual invocation for getting a reference to that object. This method is not advisable, as you will be creating an object, even before it is needed for your application. If you object loaded this way, is a heavyweight object then you will be wasting a lot of Heap memory.

   1: public class Foo{

   2:     public static Foo foo = new Foo();

   3:     private Foo(){}

   4:     public static Foo getInstance(){

   5:         return foo;

   6:     }

   7: }

Lazy Instantiation:

Lazy instantiation means, the object is initialized when the application requires it, instead of creating at the time the Class is loaded on to the JVM. Lazy instantiation can be done in variety of ways, each having subtle problems which I will be explaining shortly.

Locking: In this method we will make the get Instance method synchronized, this will acquire a lock on the Class type, and any threads that want to access the methods of this class will be kept on waiting and the performance degrades at a high degree.

   1: public class Foo {

   2:     private Foo foo;

   3:     private Foo(){}

   4:     public static synchronized Foo getInstance(){

   5:        if(foo==null){

   6:          foo = new Foo();    

   7:         }

   8:         return foo;

   9:     }

  10: }

Double Checked Locking: In this method we will check whether the object is initialized before acquiring the lock, and again check whether any thread has completed the creation of the object after acquiring the lock, and then create an object.

   1: public class Foo{

   2:     private Foo foo;

   3:     private Foo() {}

   4:     public static foo getInstance(){

   5:         if(foo == null){

   6:             synchronized(foo){

   7:                 if(foo==null){

   8:                     foo = new Foo();

   9:                 }

  10:             }

  11:         }

  12:         return foo;

  13:     }

  14: }

In this approach, there is a limited possibility of error, that is when a Thread acquires a lock and a constructor is being called. Even before the completion of the constructor, another thread might request for getInstance and see that foo is not “null”, and hence returns a partially initialized object. These sort of partially initialized objects can break the system, and will be very hard to debug.

Double Checked Locking Using volatile: In this method we will be implementing the same Double Checked Locking pattern, while adding a keyword called volatile(Introduced in JDK 1.5) to the  object variable. Volatile keyword makes sure that there is no ThreadLocal copy of this variable, and hence will always reference to the same object though it is accessed by multiple threads.

   1: public Class Foo{

   2:     private volatile Foo foo;

   3:     private Foo(){}

   4:     public static foo getInstance(){

   5:         if(foo == null){

   6:             synchronized(foo){

   7:                 if(foo==null){

   8:                     foo = new Foo();

   9:                 }

  10:             }

  11:          }

  12:         return foo;

  13:     }

  14: }

Using Inner Class: In this method we use a Singleton Helper inner class to lazily load the object, which is guaranteed to have a single reference of it across all the threads.

   1: public class Foo{

   2:     private static class FooSingletonHelper{

   3:         private static Foo instance = new Foo();

   4:     }

   5:     private Foo(){]

   6:     public static Foo getInstance()[

   7:         return FooSingletonHelper.instance;

   8:     }

   9: }

I prefer, creating the Singleton objects using this Inner Class methods, since there is no need to explicitly handle the synchronization issues, and the job of creating and making sure that only instance of the object to the compiler.

Using Enums as Singletons:

Using Enum Singletons, is increasing in todays programming, as they are more Thread-safe, and again the job of creating an object is handled by the compiler itself. While we use the Enum’s, to mark types in java having some constants usage of enum as a singleton might not sound perfect for many reasons.

   1: public enum Foo{

   2:     Instance;

   3:     public void action(){

   4:         // perfrom an action

   5:     }

   6: }


Hope this article, has given some insights to you on Singleton pattern, and was a good read. The concepts, and examples I have given in the justification are based on my opinions and experience. I would really love to hear any comments or suggestions, so please give your feedback in the comments section.

References :

  • To know more about using Enums as Singletons, go through this link.
  • More about volatile keyword can be found here.
Posted in Uncategorized | Leave a comment