Wednesday 7 December 2011

Difference Between String,StringBuffer and StringBuilder in java


Java's standard way to handle text is to use its String class. Any given String in Java is an immutable object, which means its content cannot be changed. A String has an array of characters. Whenever a String must be manipulated, any changes require the creation of a new String (which involves the creation of a new array of characters, and copying of the original array).For example

String s1=new String(”Hello”);// line 1
s1=s1+”Welcome”;// line 2

First line creates a String object in heap area with content “Hello” and makes s1 to reference this object.Second line creates a new String object in heap with content “Hello Welcome” and makes s1 to reference this new object.Now first string object with content “Hello” have no external reference and hence it is eligible for garbage collection.

Monday 5 December 2011

Difference between Vector and ArrayList in java?

ArrayList and Vector are very similar.Both of them represent a dynamically growable array ,where element can be stored and accessed using index. ArrayList is a part of the Java Collection Framework, and has been added with version 1.2, while Vector is an object that is present since the first version of the JDK. Vector, anyway, has been retrofitted to implement the List interface.

The main difference is that Vector is a synchronized object(i.e. all the methods in Vector class are synchronized) ,while ArrayList is not.

Any method that touches the Vector's contents is thread safe.That means the thread has to obtain a lock before excecuting any of the methods in Vector object.Only one thread can have the lock at a time.Once it finish the method execution ,can hand it over to other threads.Because two threads can't access one vector object simultaneously,it will create performance overhead.Hence use Vector only when you feel that ,there is a really need for synchronization.

Sunday 4 December 2011

String constant pool in Java

String constant pool is for efficient memory management in java.All the objects in java except string objects are managed in heap memory area.But in the case of string objects there is a little difference .To cut down the number of String objects created in the JVM, the String class keeps a pool of strings. Each time your code create a string literal, the JVM checks the string literal pool first. If the string already exists in the pool, a reference to the pooled instance returns. If the string does not exist in the pool, a new String object instantiates, then is placed in the pool. Java can make this optimization since strings are immutable and can be shared without fear of data corruption.
In java every object instance is on the heap, so every String instance is as well. However if you have a string literal, or string constant in your source code, java will during compilation gather all of them and pre-create them (on the heap) and store references to them in the string literal pool.