Strings in Java
Generally, String is a sequence of characters. But in Java, string is an object that represents a sequence of characters. or array of characters.
String s=”Welcome “;
or
char [] ch={‘W’,’e’,’l’,’c’,’o’,’m’,’e’};
String s=new String(ch);
How to create a string object?
The java.lang.String class is used to create a string object. There are two ways to create String object:
- By string literal
- By new keyword
1. String literal
Java String literal is created by a sequence of characters enclosed in double-quotes. For Example:
String s=”Welcome”;
Each time you create a string literal, the JVM checks the “string constant pool” first. If the string already exists in the pool, a reference to the pooled instance is returned. If the string doesn’t exist in the pool, a new string instance is created and placed in the pool. For example:
- String s1=”Welcome”;
- String s2=”Welcome”; //It doesn’t create a new instance
In the above example, only one object will be created. Firstly, JVM will not find any string object with the value “Welcome” in string constant pool that is why it will create a new object. After that it will find the string with the value “Welcome” in the pool, it will not create a new object but will return the reference to the same instance.
Why java uses the concept of string literal?
Concept of string literal makes Java more memory efficient because no new objects are created if it exists already in string constant pool.
2. By new keyword
String s=new String(“Welcome”);
In this case, JVM will create a new string object in normal (non pool) heap memory and the literal “Welcome” will be placed in the string constant pool. The variable s will refer to the object in heap (non pool).
class StringExample {
public static void main(String args[]) {
String s1 = “TAT”;
String s2 = “TAT”;
String s3 = new String(“TAT”);
String s4 = new String(“TAT”);
Get Aradhana kushwaha’s stories in your inbox
Join Medium for free to get updates from this writer.
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
}
}
Note- String objects are stored in a special memory area known as the “string constant pool”. All objects in Java are stored in a heap. The reference variable is to the object stored in the stack area or they can be contained in other objects which puts them in the heap area also.
Why string objects are immutable in java?
Because java uses the concept of string literal. Suppose there are 5 reference variables, all refer to one object “Welcome”. If one reference variable changes the value of the object, it will be affected to all the reference variables. That is why string objects are immutable in java.
For Example:
class Demo {
public static void main(String args[]){
String s=”Sachin”;
s.concat(“ Tendulkar”); //concat() method appends the string at the end
System.out.println(s); //will print Sachin because strings are immutable objects
}
}
Output- Sachin
Now it can be understood “Sachin” is not changed but a new object is created with “Sachin Tendulkar”. That is why string is known as immutable.
Two objects are created but s reference variable still refers to “Sachin” not to “Sachin Tendulkar”. But if we explicitly assign it to the reference variable, it will refer to “Sachin Tendulkar” object.
For Example-
class Demo {
public static void main(String args[]) {
String s=”Sachin”;
s=s.concat(“ Tendulkar”);
System.out.println(s);
}
}
Output- Sachin Tendulkar

![Java 25 Features [LTS Version]](https://miro.medium.com/v2/resize:fit:1358/format:webp/1*9KE-hiRO06_O0VD42zT00Q.png)




