String class in java

In recent post we have seen String in java and discussed how many ways to create a string in java. In this post, we will be discussing about Java String and String class in java.

Here is the table content of the article will we will cover this topic.
1. What is the String class in Java?
2. Properties of Java String
3. Creation of string by string literal
4. How does it work in string constant pool?
5. Creation string object by use of new keyword in java?
6. How does it work in heap memory and string constant pool?

7. Declaring string in java?
8. String initialization in Java?

9. String literal VS String by new keyword

What is the String class in Java?

Definition of string
A String is a sequence of characters or group of characters, A string meaning it can have a group of characters like “JavaGoal”, here the string “JavaGoal” containing multiple characters that are ‘J’, ‘a’, ‘v’, ‘a’, ‘G’, ‘o’, ‘a’, ‘l’. In Java, a string is represented as an object of String class that presented in java.lang package.

String class in java

Java string class exists in java.lang package and implementing three interfaces that are Serializable, Comparable, CharSequence.
The string is a class in java that is used to create the object of String type. The object of the String class always contains a string(a group of characters). Suppose you want to store your name in a variable then you can create an object of String class and assign the name to the object.

string class in java

In the above example, we are creating an object of String class and assigning a string that is “JavaGoal” to the object. There are some other ways to create an object of String class we will discuss it later.

Properties of Java String

1. As we know string is a group of character, but a java String always be an object and it can’t be a primitive type.

2. Whenever we want to create a string(Group of character), we need to an object of type String.

3. To create a string the sequence of characters enclosed within double quotes ” “.

4. In java String class is final class, so we can’t extend it.

5. The objects of String are immutable which means a constant and cannot be changed once created. If we try to change it, the JVM creates a new string object at the background. We will discuss it in detail how the string is immutable?

NOTE: Each object created by the String class is immutable. We will discuss it later in the immutable string.

How to create a string? 

There are two ways to create the string by use of String class. We can create an object of String by string literal and new keyword. Whenever we create a string, it always stores in String constant pool. Let us see how we can create an object of String in both ways.

Creation of string by string literal

The most common way to create a string by string literal. In this case, a string literal is enclosed with double-quotes. Whenever we create a string by java string literal the JVM creates an object of String. Let us see how we create a string by string literal:

String class in java

String: To create an object by string literal we use the String class.
referenceVariable: referenceVariable is the name of a variable that holds the object of String.
“string”: In simple terms, it is a group of characters, but it is considered as an object of String class.

String nameOfStudent = “Ravi”;
String s = "Hi";
String obj1 = "Hello";
String obj  = "123";

Here nameOfStudent is the object of String and the value of the object is Ravi.

How does it work in string constant pool?

Whenever we create a string by String literal it always stores in the string constant pool. The string constant pool is part of the heap memory. Let’s see how does string store in memory.

Step 1: When we create a string by use of String literal. Firstly, JVM checks whether that string is already presented in the string constant pool or not?
Step 2: If the created string already exists in the String constant pool, then JVM returns the reference of the existing string.
Step 3: If the string doesn’t exist in the String constant pool, a new string object is created in the string constant pool.

String name = “Ravi”;

When JVM execute the above statement, it performs all the operation mentioned above. Step1: Firstly, it checks the string “Ravi” in String constant pool. Step2:  But there is no string stored in String constant pool. Step3: So, now a new Object of String is created in String constant pool.

String class in java

Now, create another String object with value.

String name2 = “Ravi”;

When JVM executes the above line, it will again perform all the step again:

Step1: Firstly, it checks the string “Ravi” in String constant pool.
Step2: As you know “Ravi” is already Exists in String constant pool. So JVM will return the reference of an existing object

String class in java

Creation string object by use of new keyword in java

We can create a String by use of new keyword in java. String class has many constructors that take the characters at the different forms and create the string. We will discuss how we can create String by use of the constructor.

String class in java

String: To create an object we use the String class.
nameOfObj: nameOfObj is the name of a variable that holds the object of String.
new: It is an operator that is used to call the constructor of the String class.
string(“groupOfCharacters”): It is a constructor that accepts the string(a group of characters). You can read all the string constructor.

String name = new String("Hi");
String obj = new String("123");
String obj1 = new String("Java Goal"); 

NOTE: If we don’t provide any parameter(string) in the constructor, then it will create a blank String in heap memory. Whenever we create the String by new keyword then JVM will create a new String in heap memory and place the same string in String constant pool.

String name = new String(); 

Another example of String object:

String name2 = new String(“Ravi”);

When JVM executes the above statement, it will create the new object of String in heap memory and also place the same string in String constant pool. The value of the object is Ravi.

String class in java

How does it work in heap memory and string constant pool?

Whenever we create a string object by new keyword. It always stores in the heap memory and string constant pool. Let’s see how does string store in memory.

Step 1: When we create a string by the use of a new keyword. JVM creates a new string object in the heap memory whether it is exists in heap or not. After that JVM checks whether that string is already presented in the string constant pool or not?
Step 2: If the created string already exists in the String constant pool, then JVM doesn’t create it again.
Step 3: If the string doesn’t exist in the String constant pool, a new string object is created in the string constant pool.

NOTE: When we create a java string object by new keyword, JVM creates two objects in memory. Firstly, it creates the object in Heap memory after that it will create in String constant pool.

String name = new String("JavaGoal");
String name1 = new Sting("JavaGoal");

When JVM execute the above statement, it performs all the operation mentioned above.

Step 1: Firstly, it will create a new String object in heap memory with value “JavaGoal” and return the object reference to “name“. After that JVM will check the string “JavaGoal” in String constant pool.

Step 2:  But there is no string stored in String constant pool.

Step 3: So, now it will create a new Object of String is created in String constant pool.

Step 4: Now it will execute the second line and it will create again a new object in heap memory with value “JavaGoal” and return the object reference to “name1“. After that JVM will check the string “JavaGoal” in String constant pool.

Step 5: Now the “JavaGoal” string is already presented in String constant pool. So it will not create object again.

String class in java
public class StringExample 
{
    public static void main(String[] arg) 
    {
    	String name1 = ""; // blank string by string literal 
    	String name2 = new String(""); // blank string by new  keyword 
    	String name3 = "Hi"; // Creating string by string literal 
    	String name4 = new String("Hi"); // Creating string by new keyword 
    	String name5 = "Hi"; // Creating string by string literal 
    	String name6 = new String("Hi"); // Creating string by new keyword literal 
    	String name7 = "Hi JavaGoal"; // Creating string by string literal 
    	String name8 = new String("Hi JavaGoal"); // Creating string by new keyword 
    	
    	System.out.println("Are name1 and name2 same : "+ (name1 == name2));
    	System.out.println("Are name3 and name4 same : "+ (name3 == name4));
    	System.out.println("Are name3 and name5 same : "+ (name3 == name5));
    	System.out.println("Are name4 and name6 same : "+ (name4 == name6));
    	System.out.println("Are name1 and name2 same : "+ (name7 == name8));
    }
}

Output: Are name1 and name2 same : false
Are name3 and name4 same : false
Are name3 and name5 same : true
Are name4 and name6 same : false
Are name1 and name2 same : false

Declaring string in java

Like other variables, we have to declare a reference variable for the string. Let’s see the java string syntax:

String class in java
String s;
String name;
String obj;

String initialization in Java

We have already seen we can initialize a string at the time of declaration. Here we will see how to initialize a string if it is already declared. We can initialize it by the string literal or new keyword., If we want to use String literal then the values which we assign to the string should be enclosed with the double quotes ” ”. But if we want to initialize by the new keyword then we must choose the constructor of string class.

String class in java
String s;
String name;
String obj;
s = "Hi";
name = "JavaGoal";
obj = "Hello"
s = new String("Hi");
name = new String("JavaGoal");
obj = new String("Hello");

String literal VS String by new keyword

Different memory use

String literal always used String constant pool to store the string objects. String constant pool is part of heap memory.
On the other hand, if you are creating the string by a new keyword then JVM will create two objects in memory. Firstly, it creates the object in Heap memory after that it will create in String constant pool.

String name = “Ravi”; // Creating object in String constant pool

String address = new String(“CHD”); // Creating an object in heap memory and String constant pool.

String address1 = new String(“CHD”); // Creating an object in Heap memory but it will not create an object in String constant pool because this string is already present in<strong> pool.

String address2 = “CHD”; // It will not create any object in string constant pool because this string is already existing in the pool. So JVM will return the reference of an existing string.
String class in java

Reuse String

As you know string literal always stored in String constant pool. Whenever the user creates a string object by string literal the JVM first check the string literal in String constant pool. If the string is presented in String constant pool, then JVM returns the same reference of an existing string. It doesn’t create a new string.
If a user creates the string by use of a new keyword the JVM is forced to create a new string reference, even if a string is existing in the heap memory. It will create two objects one is in heap memory and another is in String constant pool.

String name = “Ravi”;
String name1 = “Ravi”; 
String address = new String(“CHD”); 
String address1 = new String(“CHD”);
String class in java

Leave a Comment