What is String in Java

In this article we learn the what is string in java, methods of declaring string in java, java string buffer, java string builder and also learn the why string is immutable in java.

What is String in Java?

The string in java are a collection of characters bound by double quotes (“ ”). The String is an array of characters, and since arrays are immutable in nature, a String is an immutable object, which means they are constant, and we cannot change them after creating the objects.

String can also be listed as an array of characters starting at the index 0 and ending with a null character. This means that the first letter of the string is at index 0 to the last character of the string which is indexed at (length of the string -1).

This is an individual datatype in Java.

Methods of Declaring String in Java

There are two ways of declaring strings in Java

1. By using the String literal

2. By using the new keyword

1. By using the String literal

It is the most common way to create a String. Java String literal can be created just by using double quotes.

String course = ” Welcome to Java”;

Through this line, the compiler creates a String object in a String Pool with its value” Welcome to Java”.

2. By using the new keyword

Java String can also be created by using a new keyword with the String constructor.

String course = new String (“Welcome to Java”);

Java String Buffer | String Buffer in Java

This constructor is a mutable class which means that the strings passed through this can be changed as per requirement. It has thread protection, which means multiple threads cannot access the object passed.


StringBuffer<variable> = new StringBuffer(<String>);

String Buffer objects are always created with a new operator. There are three ways by which we can create String Buffer Objects:

1. Creating an empty String Buffer object StringBuffer sBuffer = new StringBuffer ();

2. Creating and initializing the String Buffer object StringBuffer sBuffer = new StringBuffer (“Hello World”);

3. Creating a StringBuffer object with an initial capacity equal to n int n= 15;

StringBuffer sBuffer = new StringBuffer (n) ;

Here, n is equal to 15 which is the number of characters. The object sBuffer has the capacity to hold 15 number of characters.

Java String Builder | String Builder in Java

String builder is similar to a string buffer. It is a class which has mutable property. However, string builder is free from thread protection, i.e., multiple threads can access the string at the same time. It is more efficient than a String buffer.

StringBuilder<variable> = new StringBuilder(<String>);

Why String is Immutable in Java?

Whenever we create a string object using string literal, that object is stored in the string constant pool.

For Example- String s1 = “ABC”; String s2 = “XYZ”; String s3 = “123”; String s4 = “A”;

Whenever we create a string object using new keyword, such object is stored in the heap memory.

String s5 = new String(“ABC”);

char [] c = {‘J’, ‘A’, ‘V’, ‘A’};

String s6 = new String(c);

String s7 = new String (new StringBuffer ());

Whenever we declare a variable or create an object, it is stored in the memory. At a high level, Java divides the memory into two blocks: stack and heap. Both memories store specific types of data and have different patterns for their storage and access.

The String constant pool is a special memory area. When we declare a String literal (i.e without using the new keyword), the JVM stores this string in a pool of strings called the String Constant Pool.

Suppose we declared two strings of the same value “Welcome to Java”

String s1=” Welcome to Java”;

String s2=” Welcome to Java”;

JVM creates a reference s1 and points it to the object “Welcome to Java”. Whenever the compiler executes the second line which is String s2=” Welcome to Java”; it realizes that “Welcome to Java “object is already in the String Constant Pool ! So, it points the new instance s2 to the same old “Welcome to Java” object created in the first line. No new objects create.

As soon as a string object gets created, it is created in two places, one in the heap memory and the other in the string constant pool. However, when the second reference also has the same value as the one in the string constant pool the reference points to the pool memory instead of the heap memory.

Other Popular Articles:

What is Constructor in Java?

How to Reverse a String in Java

Leave a Comment