Strings , I/O and formatting (Part-2)

Welcome to the second part of Strings,IO and formatting in Java.In the previous post we have discussed about some interesting facts in Java String class.In this post we'll further talk about string class.When we talk about programming languages,one of their key goal is to make the language more memory efficient and reliable.As strings are 16-bit unicode characters when an application grows it's very common for string literals to occupy large amounts of program's memory.So to address this issue JVM sets a special area of memory called "String constant pool".When the compiler encounters a string it first check the string constant pool to check whether an identical string already exist for the string.If found it points out the string to the identical object without creating a new object.Sounds interesting right...!!!!So what's the difference between following two methods.Below you get a fine illustration on how string constant pool comes in handy in memory management. 


String s="hasitha";
String s=new String("hasitha");

In the first example we create one string object and one reference variable.But in the second one we are creating two objects and a reference variable.One object is created using new keyword in the normal memory while "hasitha" object is created and place in the pool.

So we have gone through some hidden facts in java string class.Now it's time to study some useful methods in string class. The link will help you to get some ideas about java string methods.(P.S.-No need to go through in the post.We are advanced java developers..!!!!!!!).Okay you know about Strings now.Let's talk about String buffer and String builder classes in the next post.
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html



                                                

Comments