Tutorials

Java String


Create String in Java


Most simplest and direct way to create a String is to write:

 

String name = “Rajneesh”;

Example


public class CreateString {

      public static void main(String[] args) {

            String name = "Rajneesh";  //creating String literal
         
            System.out.println(name);
      }
}

 

Here “Rajneesh” is string literal - which represents sequence of character enclosed in double quotes.

When compiler encounters String literal in your code for the very first time it immediately creates a String object with literal value. In this case it creates object with value Rajneesh.

 

Before creating a new object JVM checks String constant pool.

If the object with same value is present there then new object will not be created and work will be performed with already existing object.

 

We will learn more as we proceed.

 

Question Time: Where the object that is just created would get stored?

Answer: Literal "Rajneesh" will be stored in String constant pool.

Operation in String Constant Pool

When first time literal Rajneesh is encountered then JVM will immediately verify if any object with name Rajneesh exists in the String Constant pool or not.

If it doesn’t exist then a new object will be created with literal value (Rajneesh in this case) and stored in String constant pool.

Now the variable name will refer to object Rajneesh. Refer the diagramatic explanation given below:

 

                         String literal example by tutorialsinhand.com

 

 

Next Step: Create another literal with same value.


String anotherName = “Rajneesh”;

This time when JVM will check String constant pool it will find the object with name Rajneesh already present there.

Thus instead of creating a new object it will provide the reference of existing object to the reference variable anotherName.

Refer the diagramatic explanation given below:

                                                

                          String literal example by tutorialsinhand.com

 

Important Question: How many objects are created in our case?

String Name = “Rajneesh”;

String anotherName = “Rajneesh”;

 

We have already discussed about this so far. And we found that only one object will be created. And both Name and anotherName variable will refer to the same object Rajneesh.

In case of any doubt please go through the whole explanation once again.

 

Advantage of using String literal

  • Help improve memory efficiency in java.
  • As new object will not be created if one already exists thus it saves lot of memory as well.

 

Create String using new keyword

We can use new keyword along with a constructor to create String object. An example is given below:

 

String name = new String("Rajneesh");

Note: The String class has total 13 constructors that help us to provide initial value of String using different sources. Refer Example1 and Example2 given below to get idea of two String() constructor utilization in creating String. (Reference Oracle docs)

 

When creating String with new keyword you should keep in mind:

  • Two objects will be created.
  • Literal “Rajneesh” will be placed in String constant pool. This is already explained. For detail refer previous topic.
  • JVM will place another String object in the heap memory (non pool area).
  • Variable name will refer to the object Rajneesh in the heap area in case of new keyword usage.

Example 1: Create String using new keyword with constructor accepting String value directly


public class CreateString {

      public static void main(String[] args) {

            String name = new String("Rajneesh");         
            System.out.println(name);
      }
}

 

Example 2: Create String using new keyword with constructor accepting char array as input.


public class CreateString {

      public static void main(String[] args) {

            char[] ch = {'R','a','j','n','e','e','s','h'};
            String name = new String(ch); //converts char array to String           

            System.out.println(name);

      }
}

 

What’s next?

In next tutorial we are going learn about one of the most important and interesting topic in java. Immutability of String and the reason behind the String immutability.

 



Please Share this page
Views : 171
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!