String Class

Overview

String class in Java represent sequence of characters. Using Java String class, you can store words and sentences in a variable. We need to enclose the value of a String inside double quotations.

Declaration and Intialization

We can instantiate a String class just like any other object in Java using new keyword.

String str = new String("Hello");

However, we can also instantiate a String object without using new keyword.

String str = "Hello";

But there is a difference between both styles.

String Constant Pool

JVM allocates separate memory for String Constant Pool to maintain String literals.

JVM will create a new object every time you use new keyword. However, if you create a String without using new keyword, JVM will pull the reference of that string from a String Constant Pool, if that string is already existing in the Pool. If the String is not present in String Constant Pool, a new String object is created by JVM and placed in the Pool before returning its reference to the variable.

To show this behavior, let’s see an example.

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

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

In this example, we have initialized str1 and str2 reference variables with 2 String objects we created using new keyword. We initialize variables str3 and str4 without new keyword. We are then comparing str1 with str2 and str3 with str4. You note that, in this comparison, we are comparing the references of String objects, instead of comparing their values.

Since str1 and str2 variables have initialized with String objects created using new keyword, both will point to two different objects though the value is same. So, the first print statement will print false.

When we define str3 with “World”, it will create a new instance as there is no String object already in the heap. However, when we define str4 with value “World”, instead of creating new String object, the reference of “World” which is already existing in heap will be returned.

Strings are Immutable

Objects of String class are immutable. That means, once we create an instance of String class, we cannot modify its contents.

String str1 = "Hello";
str1 = str1 + " World";

Though above statement appear as if we are modifying the String value, it is not. We are appending another String object to create new String object. The new String object’s reference is stored in the variable.

So if we try to match previous reference with current reference it will not match.

String str1 = "Hello";
String prevRef = str1;
str1 = str1 + " World";
System.out.println(str1 == preRef);

Above println statement will print false, not because the value is changed. We do not compare value of String objects using double equal operator, instead we use equals method for it. The println statement will print false, because reference variable str1 now points to a different object created out of appending str1 with ” World”. Now, str1 holds the memory location of “Hello World”, and the original value of str1, that is “Hello” remains as is in its previous memory location which is now preserved by prevRef.

Let’s see this with another example.

String str1 = "Hello";
str1.concat(" World");
System.out.println(str1);

In the above example, we are appending another String object to str1 using concat method. But, the println statement prints only “Hello”. Calling concat method on a String object doesn’t modify the original String object, instead it returns a new String object appending two Strings as “Hello World”. Since we are not storing it in a variable, there is no way of printing “Hello World” here.

Not only concat method, all of String’s methods do not change the original value.