What are different ways to create a string object in Java?

By Preeti Jain Last updated : February 02, 2024

Here, we are discussing two ways to create the string object in Java,

  1. By using String Literal
  2. By using new Keyword

Now, we will discuss each way given above in detail with the help of an example.

String object creation using string literal

  • String Literal is a proper sequence of characters.
  • String Literal is enclosed in double-quotes from the source set of characters.
  • We should go for String Literal when we want to represent a proper sequence of characters taken together.
  • Every String Literal must be terminated with "\n" (null) character.
  • A string Literal is created in java by using double quotes(" ").

Syntax

String str = "Java Programming";

Note

  • Whenever we create a string literal, the Java Virtual Machine (JVM) first checks in the "string constant pool", so there are two cases:
    1. If, String Literal already exists in the pool then, in that case, a reference to the pooled instance is returned (i.e. no new instance is created for the existing String Literal).
    2. Else, String Literal is not already existed in the pool then, in that case, a new string instance is created and placed in the pool (i.e. new instance is created for the new String Literal).
  • String objects are stored in a special memory area known as "String Constant Pool".

Example

// Java program to demonstrate the example of
// creating string object by using String Literal.

public class CreateStringObjectByStringLiteral {
    public static void main(String[] args) {
        // Creating a string literal and placed in 
        // memory "string constant pool"

        String str1 = "Java Support OOPS Concepts";

        System.out.println(str1);

        // Again,we create the same literal ("Java Support OOPS 
        // Concepts") then in that case a new instance is not created 
        // because JVM check the "string constant pool" first before 
        // creating an object if exists then return the same object
        String str2 = "Java Support OOPS Concepts";
        System.out.println("Return the same already placed in SCP :" + " " + str2);
    }
}

Output

Java Support OOPS Concepts
Return the same already placed in SCP : Java Support OOPS Concepts

Creating a String object by using "new" keyword

Here, we will see the second category by using "new" keyword and how to create a string object with the help of the "new" keyword.

It is similar kind of other object creation by using "new" keyword.

Syntax

String str = new String("Java Programming");

In the above example, two objects are created along with one reference (i.e. one object is for "string constant pool" and another object is for "heap").

Note

  • Whenever we create a String object by using "new" keyword, The Java Virtual Machine (JVM) creates a new string object in "Heap Memory" and the literal "Java Programming" will be placed in "String Constant Pool" and the variable "str" will refer to the string object placed in "Heap Memory".
  • When we create string objects by using "new" keyword so the objects are stored in a special memory area known as "Heap".

Example

// Java program to demonstrate the example of
// creating string object by using "new" keyword.

public class CreateStringObjectByNewKeyword {
    public static void main(String[] args) {
        // Creating a string object and placed in 
        // memory "Heap"
        String str = new String("Hello, Java");

        // In the above case two new instance is created 
        // [one is for SCP and other is for Heap] along with 
        // one reference
        System.out.println(str);
    }
}

Output

Hello, Java

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.