Sunday, 29 July 2012

String objects are immutable



String is one of the most important classes in Java API.String objects are immutable. It means that once an object is created it cannot be modified.Immutable objects are automatically thread safe/synchronized.Apart from String class, all the other Wrapper classes like Integer,Boolean,Character,Byte.., etc are also Immutable.

A string object can be created either by using new operator or  by using sting literals.

String txt1 = "Text1";
// this creates an object with value "Text1" and assigns to txt1 in the string constant pool.(String constant pool is maintained by the JVM, for memory management.This way it looks for the string constant pool to find out if there is already an object with the same value, if so, it makes this reference to point to the same object instead of creating another object).

eg., if there had been another object created like this before
String txt2 = "Text1";
then String txt1 = "Text1"; will merely assign the txt1 reference variable to "Text1" object.This way there may be a lot of references pointing to the same object. If some other program is allowed to modify the object, then there might be problems for the other references.This is the reason why String objects have been made immutable. 

String txt1 = new String("Text1");
//this creates a new object with value "Text1" and assigns to txt1 in the heap.

To check the above, we can use the == operator and .equals() method.

== operator checks if the references are pointing to same object.
.equals() checks if the values are same in both the objects.

Code:
public class CheckString {
public static void main(String[] args) {
String txt1 = new String("Text1"); //object created in heap
String txt2 = "Text1"; // created in string constant pool
if(txt1 == txt2)
{
System.out.println("txt1 and txt2 references points to same  object");
 }
if(txt1.equals(txt2))
System.out.println("values are same for both txt1 and txt2");
 String txt3 = "Text1" ; // in String constant pool as already       there is "Text1" object, txt3 is made to point to it instead of     creating new one.
 if(txt2 == txt3)
   System.out.println("txt2 and txt3 points to same object");
 if(txt2.equals(txt3))
   System.out.println("txt2 and txt3 values are equal");     
 }
 }

Solution:

values are same for both txt1 and txt2
txt2 and txt3 points to same object
txt2 and txt3 values are equal


Java Heap Space:
Java's Heap space is the memory what JVM borrows from operating system and allocates for our program.Every object created with new operator is stored in this heap space.Most of the unused objects are garbage collected. But, some times we do encounter,  java.lang.OutofMemory error in our webserver console.To solve this, we can increase the heap size by overriding the -Xms and -Xmx parameters. It mentions the minimum and maximum memory size respectively.Restart the web server and the error goes away..

java -Xms<initial size> -Xmx<maximum size> program_name
eg., java -Xms64m -Xmx128m CheckApp
However if the heap size is set greater than our computer's physical memory, it leads to some error like

Error occurred during the initialization of the JVM.
Could not reserve enough space for object heap
could not create the Java virtual machine.


Solution:
Reduce the heap size lesser than the physical memory.


If the outofMemory does not go away , then we may have a memory leak. Memory leak is nothing but we have an object existing in the memory without reference, which will never be used again and it does not get garbage collected.





No comments:

Post a Comment

Got to say something?...Leave your comments here