String in Java – String Literal, String Pool, String Objects

String in Java – String Literal, String Pool, String Objects

         String is a special non primitive data type in java if we compare with any others. But why! In String, we are having string pool as well as heap memory to store the values. The strings are immutable by nature. String is an object which contains sequence of character’s. String is not belong to primitive data type whereas we can create object using java.lang.String. There are two ways to create the string in Java.

             1. By Literal in Java

             2. By New Keyword

String Literal in Java

String Literals are very special in Java. We can create String object using double quotes as followed,

String message = "Hello ! I am a string";

The above code calls the intern() method of string first. Then the intern checks whether the same object is exists in pool or not!. If it is exist, it will return the same object reference. Otherwise It will create the new object in the pool and return the new object reference. Ultimately the string literal should be fast if we compare with new keyword since it will not create the new objects every time.

String Literal

By New Keyword

We can create String object using new keyword as followed,

String message = new String("Hello ! I am a string");

The above code creates two string objects. It will create one object in Heap Memory directly and other in String Pool Area. And the reference will points to heap memory object.

new of string in java

Caution in String Compare

We have to use equals method of string to compare two strings always, not ==. Because == compares only the memory locations. But the equals method compare the memory location as well as the content of the stored objects.

String a = "Hello";
String b = "Hello";

// since literals re-use existing objects from the pool
System.out.println(a == b) // true

String c = new String("Hello");

// new keyword always creates new objects in the heap
// And == compares the memory locations not the exact
// content of the objects
System.out.println(a == c) // false

// Always use equals for comparison
// String class equals method checks the content of the 
// object as well
System.out.println(a.equals(c)) // true

Why String is Immutable

The rule to create any immutable object is, it’s state can not be changed after it is created. And String class designed to follow this rule. Even we could create N number of objects by following this rule. Let’s come to the point, why string is immutable ? The most correct answer is probably: Because the java language authors or designers figured it was a good idea. So that It will provide lot of benefits to us, Let’s see one by one.


We have been mostly using string object in network connections, database connection URLs, usernames/passwords and etc. It it were mutable, we can change these parameters and runtime And which creates security issues.

Synchronization And Concurrency

We can say an object is thread safe, when multiple threads are operating on it but none of them is able to corrupt its state and object hold the same state for every thread at any point in time. Making String immutable automatically makes them thread safe by default thereby solving the synchronization issues.


when compiler optimizes your String objects, it sees that if two objects have same value (a=”test”, and b=”test”) and thus you need only one string object (for both a and b, these two will point to the same object)

Hash Code Caching

String object caches its hash code at the time of object creation which makes it the great candidate for hashing related operations because hash code doesn’t need to be calculated again which save us some time. This is why String is mostly used as Hash Map keys

Class Loading

We have been using String as an arguments for class loading. If mutable, it could result in wrong class being loaded (because mutable objects change their state).


  1. Usually we design classes as immutable for them being thread safe and to optimize performance and memory consumption.

  2. Security and Efficiency to be the primary design goals, then immutable strings are a means to meet those goals.

  3. immutable objects in java are like copy-by-value, you can have 2 references to a String, but you should consider them 2 separate Strings since it’s immutable, and working with one of them won’t affect the other.

You May Also Like

Leave a Reply

%d bloggers like this: