String Type- Literal, Non-Literal, Mutable, Immutable

 String Types

--------------

Literal String 

Non literal String


Immutable

Mutable


Literal String:

---------


String s1 = "welcome";

String s2 = "welcome";

Incase of the duplicate, it will share the same memory location.

It is stored inside the string pool or string constant


System ---->identityHashCode()---> memory address


Non Literal

--------------------

String s2 = new String ("welcome");

String s3 = new String ("welcome");

Incase of duplicate It will not share the same memory location. it will share the different memory location. 

.

It is stored in the heap memory.

-------------------------------------


Immutable

---------

same as the literal string

String s="welcome";

we use concat method.

While join or any action on the immutable String, it will not share first string  memory value


Mutable

-------

same as non literal string but use StringBuffer or StringBuilder


StringBuffer s = new StringBuffer("welcome");    //   StringBuilder s = new stringBuilder("welcome");


we can use append method

While join or any action on mutable string it will share first string  memory location



StringBuffer 

============                                

Synchronous --> Sequential

Threadsafe

slow                                                                   


StringBuilder

=============

Asynchronous --> Non Sequential

Non Thread safe

fast


Comments

Popular posts from this blog

Inheritance and Type of Inheritance

DAY 1: Java Course Notes

Interview Topics for Software Testers and QA