Strings in Java: A string is one of the important concepts of Java programming language because its a commonly used java class library. In General, String is nothing but a sequence of characters, for example, “SoftwareTestingo,” “Testing.” But in Java, the string is an object which is created using the string class. The string objects are immutable; that means if they are created once, then they can’t be modified, and if you make any changes in the object, then that will create another new object. We can locate the string class inside the java.lang package.
What is the Strings in Java?
We Can create a string object in java two ways:
- By String literal
- By New Keyword
By String Literal
We can create a string by assigning a string literal to a String instance like below
String str1=”SoftwareTestingo”;
String str2=”SoftwareTestingo”;
If we created a string in this approach, then there is a problem. As we have already discussed that string is an object, and also we know that we are creating an object by using the new key work, but you can see the above statement we are not using any new keyword.
On Behalf of us, Java compiler does the same task and creates the string object and stores the literals, which is “SoftwareTestingo” and assigned to the string instances or objects. and also, the objects are stored inside the string constant pool.
String literal/constant pool: This is a special area of heap memory which is used for storing the String literal or string constants. So whenever you create a string object, then JVM first checks the string constant pool for the literal. If JVM found the literal in string constant pool, then, in that case, JVM is not going to create another object for the literal in the constant pool area. It only returns the already existed object reference to the new object.
If the object is not present in the constant pool, then only a new instance is going to created and placed on the pool area.
So in the below code :
String str1=”SoftwareTestingo”;
String str2=”SoftwareTestingo”;
In the above example, in the case of str1 JVM checks for “SoftwareTestingo” in string constant pool. The first time it will be not in the pool; hence, JVM created a new instance and placed it into the pool. In the case of str2, JVM will find the “SoftwareTestingo” in the pool. Hence no new instance will be created, and reference of the already existing instance will be returned.
By New Keyword
We can also create the string object by using the new keyword. But when you create a string object using the new keyword, then JVM creates a new object in the heap area without looking on the string constant pool.
If you create a string object like below:
String str1= new String (“SoftwareTestingo”);
String str2= new String (“SoftwareTestingo”);
Then, in this case, two different objects are created, but both the objects are the point to the same literal that is “SoftwareTestingo.”
Why are string objects immutable in java?
As we have discussed above string literal and string constant pool, we get to know that n number of the variable can refer to one object. So if you change the value by one reference variable, then it affects another reference variable because all share the same object value (in Our case “SoftwareTestingo”). That’s why in Java programming language Strings are immutable.
Leave a Reply