Thursday, December 24, 2009

Java’s String pooling


A string pool is a collection of references to String objects.
Strings, even though they are immutable, are still objects like any other in Java. Objects are created on theheap and Strings are no exception.
So, Strings that are part of the "String Literal Pool" still live on the heap, but they have references to them from the String Literal Pool.

When a .java file is compiled into a .class file, any String literals are noted in a special way, just as all constants are. When a class is loaded (note that loading happens prior to initialization), the JVM goes through the code for the class and looks for String literals. When it finds one,  it checks to see if an equivalent String is already referenced from the heap. If not, it creates a String instance on the heap and stores a reference to that object in the constant table. Once a reference is made to that String object, any references to that String literal throughout your program are simply replaced with the reference to the object referenced from the String Literal Pool.
If you use the new keyword, a new String object will be created. Note that objects are always on the heap - the string pool is not a separate memory area that is separate from the heap.
The string pool is like a cache. If you do this:
String s = "abc";
String p = "abc";
then the Java compiler is smart enough to make just one String object, and s and p will both be referring to that same String object. If you do this:
String s = new String("abc");
then there will be one String object in the pool, the one that represents the literal "abc", and there will be a separate String object, not in the pool, that contains a copy of the content of the pooled object. Since String is immutable in Java, you're not gaining anything by doing this; calling new String("literal") never makes sense in Java and is unnecessarily inefficient.
Note that you can call intern() on a String object. This will put the String object in the pool if it is not already there, and return the reference to the pooled string. (If it was already in the pool, it just returns a reference to the object that was already there). See the API documentation for that method for more info.
Java String intern Function
public String intern() 
Returns a canonical representation for the string object.
A pool of strings, initially empty, is maintained privately by the class String.
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.


Tuesday, December 22, 2009

Inversion of Control ( IOC ) Design pattern

Inversion of Control (IoC) design pattern  Basics
The Inversion of Control (IoC) pattern is also known as Dependency Injection.
The basic concept of the Inversion of Control pattern is that programmers don’t need to create the objects but describe how they should be created.
IOC  design pattern  alleviates  application objects from the responsibility of creating, and managing their dependencies. Instead, these application objects are free to concentrate solely on fulfilling their business purpose .

Types of IoC
     Interface Injection (Type 1)
     Setter Injection (Type 2)
     Constructor Injection (Type 3)

Some frameworks implementing IOC


For more detailed Information on IOC