Java Concepts Notes
Java Concepts Notes
String Concept
Firstly we need to understand that String has separate memory location inside heap known as
String Pool which is divided into 2 areas.
1. String Constant Pool (SCP)
2. String Non Constant Pool (SNCP)
Now when we are creating a String object, an instance is created inside String Non Constant
Pool area and the reference (s1) will be pointing to this location and also 1 more String object will be
created inside String Constant Pool but the reference (s1) will not be pointing to this location. Both the
memory areas has "hello" as String value.
:
:
s1 --------> string value = "hello" : string value = "hello"
String Non Constant Pool : String Constant Pool
:
String s2 = "hello123";
First the compiler will check is there any value with "hello123" in String Constant Pool, if there is
no value then it will create a new value and s2 will point to that value, if there is a value matching then
no new value will be created and s2 will point to that existing value only.
:
s1 --------> string value = "hello" : string value = "hello"
: string value = "hello123" <-------- s2
:
String Non Constant Pool : String Constant Pool
Now when we are creating 2 String literal with value = "hello" as shown below
String s3 = "hello";
String s4 = "hello";
First the compiler will check is there any value with "hello" in String Constant Pool, as there is a
value matching then no new value will be created and both s3, s4 will point to the existing value only.
:
s1 --------> string value = "hello" : string value = "hello" <-------- s3, s4
: string value = "hello123" <-------- s2
:
String Non Constant Pool : String Constant Pool
Now when we are creating a new String object with value = "hello world" as shown below
A new object will be created in Heap area and the reference (s5) will be pointing to this
location, and also a String literal will be created in String Constant Pool as this value is not already
existing in SCP.
:
s1 --------> string value = "hello" : string value = "hello" <-------- s3, s4
s5 --------> string value = "hello world" : string value = "hello123" <-------- s2
: string value = "hello world"
:
String Non Constant Pool : String Constant Pool
Important Note:
1. String Constant Pool will not contain any duplicate values.
2. From the above concept you can understand that String is Immutable i.e., as the value changes by
any reference in SCP all other references which are pointing to that location will also get changed
automatically which is known as Immutability.
3. == operator is used to check the memory location (address) of any 2 objects of same type or
atleast sub class of other object which is used to compare, otherwise it throws compile time error.
4. equals() method is overriden in String class in order to compare the String values for any given 2
Strings.
5. String objects are immutable (ie., object is created as final implicitly) that is why when we are trying
to change or modify the string value it will create a new object with same reference and then assigns
new value to it.
Advantages: High performance
Disadvantages: Immutability