String Class

String class in Java

String

String in Java is a sequence of characters and it’s a non-primitive data type, that is the size of a String is not fixed.

The parent class of all classes in Java is Object and String is a class that extends the Object class and implements CharSequence, Serializable and Comparable interfaces.

Syntax of String class:

public final class String

{

// your code

}

There are three main classes by which we can create String:

  1. String
  2. StringBuffer
  3. StringBuilder

another way to create a String is:

String s = new String();

Memory allocation for String in java:

The following two areas where String values are stored by JVM.

  1. String constant pool(SCP)
  2. Heap Memory

String Constant Pool:

It is also known as String Literal Pool. It presents inside Heap Area. until version 1.6 it was present inside the Method area.

String constant pool is an area in Heap Area where all String literal values are stored by creating Objects.

Case 1:

Consider,

String s1 = new String(“hello”);

When we create a string using a new keyword, then that literal value will be stored inside the Heap Area inside an object, and also same value will be stored inside SCP as an object but, before stored value in SCP, JVM will check whether the same name of literal value present in SCP or not, if it is not present then only object containing literal value will be created by JVM and for that object reference will be created by JVM only for future use.

But only for those objects which are created due to a new keyword

Case 2:

String s2 = “world”;

In this case, the s2 has the literal value “world”, and the object will be directly created in SCP only, and for that object, reference will also be created.

Special Cases:

Case 1:

Consider,

String s1 = new String (“hii”);

String s2 = new String (“human”);

String s3 = new String (“hii”);

above three Strings are created using a new keyword. But String s1 and s3 have the same literal value. So for these Strings, a separate object will be created inside Heap and SCP?

No, as these Strings are created using a new keyword so as per rule, an object in the Heap Area will be created for each String, even if a literal value is the same. But in SCP object creation will be different as for s1 first it will check if any object with the same literal value is present or not in SCP, if not then only JVM will create a new object, but for s3 JVM will check if the same value is present or not before creating an object. If a literal value is present then JVM will directly create a reference for that object but for its internal use.

Case 2:

Consider,

String s1 = new String (“hii”);

String s2 = new String (“human”);

String s3 = “hii”;

In this case, the object will again be created in Heap Area and SCP?

No, in this scenario, for s1 in Heap Area object will be created but in SCP, JVM will check if any object has the same literal value is present or not. if not present then the object will be created but reference will be given by JVM internally for future use.

for s3 no object creation will happen in the Heap as an object will directly be created in SCP, but if the same literal value containing by an object is present in SCP then no new object creation for s3 will happen. As due to s1 there is already one object created and that object has reference given by JVM internally, now for s3 that JVM reference will be removed and a new reference will be created because of s3 pointing to that object.

Why String is Immutable?

Immutable means Unchanged.

In Java, String is immutable. That is once we assign any literal value to String then we cant change or modify the value of that String literal.

eg.

String s1 = new String (“India”);

For this above String, object creation will be done in the Heap Area as well as in SCP.

but when we try to concate another literal with above-existing one String literal like this,

s1.concate(“Country”);

It is not possible to concate another literal value with the existing one. But Java will make a new object for “IndiaCountry “, Which was created due to the concate method. i.e. object with a new literal value will be created in Heap Area but no reference will be created to that object as we not assigned a new literal value to String i.e to s1.

The reference for this new literal will be assigned if we assign this literal to String like this if we do,

s1 = s1.concate(“Country”);

Then in this case reference created for the s1 object in Heap Area when it has a literal value “India” will be removed and now the reference will go to the new object that has a literal value “IndiaCountry”.

and hence we can say,

String immutability is related to String objects.

Q. Why String Objects are Immutable?

Consider following code,

String s1 = “Mumbai”;

String s2 = “Mumbai”;

String s3 = “Mumbai”;

In this case, all Strings have the same literal value and object creation will directly happen in SCP only. As literal value is the same so, In SCP only one object will create for s1,s2, and s3. But three different references will be created for each String.

Suppose, If we modify the literal value of s3 and make it “Chennai “ then it will not affect to current object as Java will create a new object having a literal value as “Chennai” and remove the old reference for s3 from an old object and set a reference variable to that newly created object. that’s why String objects are immutable in Java.

--

--

--

Full Stack Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Java Variables

Building a Forest with Functions

images/moving_players/when_you_are_done.png

Amazon EKS security Best Practices

The Interface Segregation Principle — it’s confused

Using the ternary operator in Ruby

Why Discord is switching from Go to Rust

Preventing duplicate executions across the system in golang

Winner List of Hurricane Effect Program

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pooja Babar

Pooja Babar

Full Stack Developer

More from Medium

Importance of ‘null’ in JAVA

Java code conventions

A problem in Inheritance (OOP) — First Part.

Exception In Java