java string

Java String – What’s so different in it?

Java String, a class which is all different than the other classes and variables, and yes you heard it right – String is different than other variables. So what’s so important and big thing about String. Let’s first try to understand the Variables in Java. Variables are the basic building block of any programming language and they are used to store data which we manipulate or work upon in the program. If you are an experienced Java Developer, you might be knowing about the Java’s Heap and Stack Memory concepts, else read about it.

The variables in the Java are stored into the Stack Memory but the Objects of a Class, which String is, get memory allocated in heap and its reference variable in the stack. So we can see that String is totally different than the primitive variables as its object gets instantiated when we want to save some string data and it goes into heap. But still you might be thinking what so great about String, obviously it has to be like that as its a class, but wait here comes the main concept.

Java String is Immutable:

String is created or instantiated or declared using any of the following syntax:

String name = new String("talent cookie "); // One way of instantiating
String motto = "where talent gets nourishment"; // Another way

Now just read this carefully: Strings are Immutable!!!

And you must be thinking, what does this Immutable means? It’s straight and simple, that Strings once defined can’t be changed. Let me explain it. So we defined two Strings in above declaration and those are than created with their reference saved in name and motto. Let’s now perform some operations on these two. The Java String class provides so many methods using those we can perform so many operations on the String.

name.concat(motto); //Here we are concatenating two strings 
System.out.println(name); // talent cookie

What just happened above is we added the motto behind the name, which should result in “talent cookie where talent gets nourishment”. Instead it gave the output as “talent cookie “. Because concat() method do not acted upon name as name is Immutable and cannot be updated/changed.

Let’s us do this instead:

name = name.concat(motto); //Here we are concatenating two strings 
System.out.println(name); // talent cookie where talent gets nourishment

Now when we concatenated two strings, we saved it into name. And what just happened is that the value of name was De-referenced and  a new reference to new value is saved into the name.

java string example

I hope above diagram would have explained it to you that what just happened in the previous code.

We will now discuss about the key feature of Java String, its memory management and how JVM manages it.

Java String Memory management:

JVM provides a separate memory to Strings called as String Constant Pool. What is this String constant Pool? Whenever a new String object is created the String is added into the String Constant Pool, i.e. a pool of all Strings available to Java program. And why does Java do so? To manage the memory efficiently. The compiler consolidates the string literals in a class for uniqueness and than the JVM loads those String literals into String pool when class file is loaded. When you ask for a String object to be created, JVM checks if a String already exists in the constant pool, if it exists it gives its reference back else it creates one and gives the reference. So that’s why String is Immutable. Imagine the condition when you can change the object value i.e. string and it is referenced at multiple places, how much havoc it would have created. Also String class is marked as final so that no one can change its functionality.

Java String deep understanding with an example:

Till now you might have understood the explanation of question: What’s so different in it? But let’s make it more clear to you with an example, which will bring more clarity.

String n1 = "talent "; //Line1
String n2 = n1 + "cookie "; //Line2
n1.concat("where "); //Line3
n2.concat(n1); //Line4
n2 += "where talent gets "; //Line5
System.out.println(n2 +"nourishment"); //talent cookie where talent gets nourishment


Line1 – String object gets created which places “talent “ in string constant pool, and saves it reference in n1.

Line2 – Two String objects gets created. One with String “cookie “, which gets into pool but its reference is lost and not saved, and another with String “talent cookie “ whose reference got saved into n2.

Line3 – Two String objects – “where “ and “talent where “, with two lost references.

Line4 – Another String object “talent cookie talent “, with a lost reference.

Line5 – Two String objects “where talent gets “ and “talent cookie where talent gets “, references to these are lost and saved into n2 by overwriting the earlier reference to “talent cookie “, respectively.

So in total 8 objects get created in these 5 lines, which gives a clear explanation about how Java String works.

Java String – last but not least fact:

You might have seen the following code in the starting, and would have thought why we have two different ways of instantiating:

String name = new String("talent cookie "); // One way of instantiating
String motto = "where talent gets nourishment"; // Another way

Here is the answer, in first case the String Object is created out of pool in the heap and its reference is saved into variable name. Also the string literal “talent cookie “ goes into pool too. Thus Two objects are created in this but only one reference is saved. In second case, the String Object is created with string literal “where talent gets nourishment” in the String Constant Pool, if it is not present there, and the reference is saved in variable motto.

So that’s all about Java String. I hope everything is understandable, if not you can comment here or connect with us on Facebook. 🙂

Related Posts

Leave a Reply

Your email address will not be published.