A string is one of those Object which is used heavily in Java. And this is the reason why String has unique handling in Java(String Pool).
- The String class represents character strings.
- All string literals in Java programs, such as “abc”, are implemented as instances of this class.
- Strings are like constants, once created its value can not be changed.
- String objects are immutable and hence it can be shared.
- String Buffer and String Builder can be used in place of String if a lot of String Operation is to be performed.
There is two way you can create strings in java. Both looks the same but there is a difference between both approaches.
String jbt = new String("JBT"); String jbt = "JBT";
Important Aspect of String
As you must be knowing, everything in java except primitives is Object. This is true for String too. A string is also an Object, hence it will reside on Heap only. But there is one more term used for String storage and that is String Pool/ String Literal Pool. Most of the time people think of it as a separate pool of String Object(True for me too). But this is not true. String Pool/ String Literal Pool is nothing but a collection of references to String objects. As we know that String object is immutable it’s good to “share” the same String object with multiple references. (Vice versa is true also as String objects are shared between different references that is the reason String Objects are made Immutable.)
String Literal & Object
Strings are created in the following two formats which differ from each other in a lot of ways.
- String Literal
- Using New keyword
So when we are saying String literal, we are actually referring the reference in String Pool and when we are saying String Object(Using new keyword) we are directly referring to the String Object in Heap. But in both cases, we are referring to the Object in Heap only(Directly/ Indirectly).
How to Create
Now the question arises how to create String Literal and String Object. As we know that we can create a String Object in 2 ways.
Using the new Keyword
String str=new String("javabeginnerstutorial");
When we are using the new operator to create String, we are actually creating the String object in Heap like in the First option. Here we are creating a String Object in Heap memory with value as “abc”. and a reference named “str”.
Also note that as we are using “”(literals) for creating this String Object, a literal will also be created. In total there will be 1 String Object 1 String literal and reference will be created. But reference will refer only the String object in Heap and not the literal in String pool.
And when we are using “” we are creating the literals(Everything inside “” are treated as literals). In this case(2nd scenario) JVM will look for all the references in String Pool and check if any of them pointing to a String object in heap with value “abc”, if it finds any, it will return the reference of that object. Otherwise, JVM will create a new String Object in Heap and interned it (Using inter() method) in String Pool(Reference for this object will be added in String Pool) for later reference. And reference of the newly created object will be returned. Now if we again write the same code to create String literal.
These types of strings are created in the Heap and if the String constant pool has an object with the same value, the object in the heap would be referred to that object in the String constant pool. The reference variable “str2” will be pointing to the object in the heap.
The String constant pool is a pool which stores unique string object thus enforcing the immutability i.e. even when you do concat on a previously created string, a new object is created both in the heap and the constant pool(provided there was no similar object present there before) and that object is returned accordingly to the program.
Garbage Collection for String
You might have heard that String literals are never eligible for Garbage Collection. Note we have mentioned String literal and not String Object. There is a difference. An object is eligible for garbage collection when it is no longer referenced from an active part of the application. String literals always have a reference to them from the String Literal Pool. That means that they always have a reference to them and are, therefore, not eligible for garbage collection. But there may be some String Object which doesn’t have any reference then they will be eligible for garbage collection.
- String Object is used for holding String of text.
- Part of java.lang package.
- Strings can be added by using concatenation Operator(+).
- Characters use single quotes(‘ ‘) as the delimiter while Strings use double quotes(” “) as delimiters.
- String objects are immutable. Value of the string object cannot be changed once assigned.
- String class is final means method of the String class cannot be overridden.
- All String literals are added to String pool by JVM.
- String have method length() while Array has a property length to check the length.
- To overcome the immutable property of String StringBuffer and StringBuilder comes into the picture.