Learn String Literal In Java

Hey guys!! Welcome to flower brackets blog. In this post we are going to learn string literal in java.

What are string literals?

Well, string literal is a sequence of unicode characters and these characters enclosed between double quotation marks is called string literal. In java, string is an object that represents a sequence of characters.

In java, javac compiler converts all string literal characters into unicode characters as the bytecode file, that is, .class file is created.

For example:

String strLiteral = "Do you want to learn java!!!?";
String strExample = "Yes I do!!";

Above we have two string literal and it consists of series of characters inside the double quotation marks. String literal may contain any Unicode characters like UTF – 16, UTF – 8 characters.

The java.lang.String class is used to create object. String literal can include escape characters like this,

String strNew = "Heyy guys!!, \"How are you doing?\"";
System.out.println("Well, \n Welcome to Flower Brackets \n");

Above you can see string literal which contains escape character code.

Java program : learn string literal in java

We will see one java program,

public class StringLiteralExample
{
   public static void main(String[] args)
   {
      String strLiteral = "Do you want to learn java!!!?";
      String strExample = "Yes I do!!";
      String strNew = "Heyy guys!!, \"How are you doing?\"";
      String learn = "Java is the best programming language.!!";

      System.out.println("strLiteral - " + strLiteral);    
      System.out.println("strExample - " + strExample);   
      System.out.println("strNew - " + strNew);   
      System.out.println("learn - " + learn);  
      System.out.println("Well, \n Welcome to Flower Brackets \n");
   }
}

Output:

strLiteral – Do you want to learn java!!!?
strExample – Yes I do!!
strNew – Heyy guys!!, “How are you doing?”
learn – Java is the best programming language.!!™
Well,
Welcome to Flower Brackets

In the above example “slash n” ( \n ) creates a new line in the output.


What is String Constant Pool in Java?

String constant pool in java is the pool of string objects stored in java heap memory.

Let us understand string constant pool using an example,

Learn String Literal In Java

String str1 = "Hello";
String str2 = "Hello";
String str3 = "Hi";
String str4 = new String("Hello");

In java programming if we declare a string literal (as shown above) we are calling intern() method. This method creates an exact copy of heap string object in string constant pool.

In the image above you can see java heap. Inside java heap we have string constant pool where pool of strings have been stored.

Now I’m declaring string object using string literal. Here JVM checks whether this string “Hello” is present in the string constant pool or not.

For explanation purpose, Initially string constant pool will be empty.

It is not there. So JVM will create new string instance and place it in the string constant pool and returns the reference.

And str1 will point to the string object in the pool. Again I will create string object using string literal.  JVM checks whether this string “Hello” is present in the string constant pool or not.

If you notice “Hello” string is present in the string constant pool. So it will just returns the reference and str2 will point to the same string object in the string constant pool.

Here now we will create another string object using string literal again JVM will check whether this string “Hi” is present in the string constant pool or not.

Also Read – Learn Strings In Java

If you notice the string “Hi” is not present in the string constant pool. So JVM will create a new string object in the string constant pool and returns the reference.

str3 will point to string “Hi” in the string constant pool. Lastly we create string object using new keyword and String constructor.

So what JVM will do is it will create string object in the java heap and str4 will point to the string object which is in the java heap.

If you check the equality operator,

str1 == str2 will return true. Because both str1 and str2 are pointing to same string “Hello”.

str1 == str3 will return false. Because str1 points to string “Hello” and str3 points to string “Hi”.

str1 == str4 will return false. Because str1 points to string “Hello” which is in string constant pool but str4 points to string “Hi” which is in java heap.

Now let’s see java program on string constant pool,

public class StringConstantPoolDemo 
{
   public static void main(String[] args) 
   {
      String str1 = "Hello";
      String str2 = "Hello";
      String str3 = "Hi";
      String str4 = new String("Hello");

      System.out.println("str1 == str2 : " + (str1 == str2));
      System.out.println("str1 == str3 : " + (str1 == str3));
      System.out.println("str1 == str4 : " + (str1 == str4));
   }
}

Output :

str1 == str2 : true
str1 == str3 : false
str1 == str4 : false


Difference between string literal and string object

Let’s understand difference between string literal and string object. As we all know objects are dynamically allocated on heap like this,

Learn String Literal In Java

Here referencing variables are on stack. Since in java, string is an object it is also stored in heap. This depends on how we create a string.

When we create a string without new keyword as string literal like below,

String a1 = "Lion";

String a2 = "Lion";

Then the string is created in the string intern pool. String intern pool or string constant is a memory area on heap where distinct string objects are stored.

When we create string, (as shown above) at compile time the compiler looks for “Lion” string in intern pool. If the string is present, it returns its reference.

However if it is not present in the string intern pool it will create that string and it will return its reference. For instance in the above declaration string (String a1 = “Lion”;) was not present in the string intern pool.

So it created one string “Lion” and it returned its reference. Now in the second declaration (String a2 = “Lion”) the compiler looked for the string “Lion” in string intern pool.

It was already present in string intern pool. So it will not create a new string. It will return the reference of “Lion” string.

We can see that both a1 and a2 have same address of string “Lion”. Now we create another string as shown below,

String a3 = "Bear";

Initially there is no such string as “Bear” in the string intern pool. So a new string will be created called “Bear” and its reference will be stored in the variable a3.

So we saw that a1, a2, a3 are variables storing string references or addresses where these particular strings are stored in heap.

When we use equality operator like,

a1 == a2

This will return true. Because both a1 and a2 are storing same address in stack. So equality operator checks whether both are storing the same string or not and hence it returns true.

But if we use equals method like this,

a1.equals(a2)

It will check whether a1 and a2 are pointing to the string intern pool. And we can see both a1 and a2 are pointing to the same string, that is, “Lion”. Hence returns true.

If we use equality operator to check if,

a1 == a3

We can see a1 has memory reference of 645 and a3 has memory reference of 521. So both are not equal and it will return false.

If we check using equals method,

a1.equals(a3)

Again it will return false. Because a1 is referring “Lion” and a3 is referring to “Bear”. So string at references are different.

Basically equals method checks whether string is equal or not. While equality operator checks the references that is stored in the variable references.

Now moving to string object when we create string using new operator like below,

String a4 = new String("Bear");

This string is not stored in string intern pool. It will be stored in other area of heap. While there was already “Bear” string present in string intern pool, because we used new keyword, JVM is forced to create new string at runtime.

Instead of choosing one from the string intern pool. We saw that string “Bear” was already present in string intern pool because we used new keyword it created another one on heap.

So as a4 stores the reference of “Bear” on heap that is 961. If you use equality operator and check if,

a3 == a4

We will find that it returns false. Because a4 is pointing to a memory location 961 and a3 is pointing to 521 and that is in string intern pool.

Both are not equal so it will return false. But,

a3.equals(a4)

Using equals method we get result true. Because we are checking value at the address at a3 and a4 and the value is same. Hence return true.

NOTE :

Both string literal and string object are created on heap. But string literal belongs to the special area where only unique strings are allowed to store.

String literal is preferred when we know the value of string at compile time so one doesn’t have to create one at runtime.

What happens is when (dot java) .java file is compiled to a (dot class) .class file, JVM scans for string literals just like it does for constants.

It then checks if a reference is already exists in string intern pool. If not, then it creates one and later if anywhere in the code the string is referred it assigns the same reference.

So this helps in increasing the efficiency.


Conclusion

This was all about what are string literals, what is the string pool in java and what is the difference between string literal and new keyword.

I hope you guys have understood the concept. You can subscribe to my blog flower brackets if you haven’t already.

Do share this post if you like.

Related Posts