Strings are most widely used in Java programming.
Strings basically represent a sequence of characters.
In the Java programming language,
strings are objects.
The Java platform provides the String class to create and manipulate strings.
‘c’ is the character array having sequence of characters and same is represented using string as
String object is required to represent any string.
In above example “s” is a reference variable pointing to String object which holds “Hello” in it.
There are 2 ways to create String in Java
1) By using Literal
2) By using “new”
String creation using Literals
In this case, we just assign String value using double quotes as below
This will create the string in String constant pool (SCP) which is a separate memory used within heap.
How String constant pool (SCP) works?
Whenever we 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 string doesn’t exist in the pool, a new string instance is created and placed in the pool.
- String s1=”java”;
- String s2=”java”;
- String s3=”java”;
String s1=”java”; String s2=”java”; String s3=”java”;
As we can see in the above memory diagram, initially string
“java” is not available in String constant pool and hence it creates one in the pool.
In subsequent lines, it will find the string with the value
"java" in the pool, so it will not create new object but will return the reference to the same instance.
For above 3 lines of code,only one string object is created.
String literals created using double quotes like above will be stored in a separate memory called String constant pool.
String creation using “new”
We can create string using “new” as below
We use “new” to create any object in java and here also we are creating String object.
We know that, all objects are stored in Heap memory including String objects.
The only extra thing in case of String is, String literal =
“string value”will be placed in the String constant pool if it’s not exist in pool.
But remember, “s1” still points to an object in Heap (not to string literal in pool)
This line creates 2 objects,
1 in the Heap memory and
1 in the String constant pool
Every time we create string using “new”, Java creates new string object in the Heap and it will place the same string in String constant pool if it’s not exist in the pool.
This line creates only one object in Heap as
“ java ” is already present in String constant pool placed while creating “s1”.
Why Java has provided String constant pool mechanism?
We know that, we can create string object using “new”
Why another way of creating String object using literals is provided in Java?
Since we use Strings a lot in the java applications, Java has provided an efficient mechanism where no new objects should be created when we create same string value in multiple places of our application.
This way it helps in saving lot of memory when we refer similar strings in different parts of the application.
Since String is most widely used object in any application, they have introduced this mechanism only for Strings.
Should I use String literal or new to create String in my Project?
Always prefer creating strings using literals as its performance is much faster than creating string using “ new ”.
And also, when we create string using literals, we don’t create extra object in heap rather we refer existing object in string constant pool.
How garbage collection works with Strings?
String literals gets stored in string constant pool and its always required to be in the pool so that it can be used for future references, so string literals stored in constant pool will never be garbage collected.
However, String objects created using “new” will be garbage collected when they don’t have any reference to them.