String in Java

Jaspreet Singh
McAppMedia
Published in
Dec 18, 2019

String is a sequence of character in Java . To Create String in Java, there are 2 ways -

First is to Create a String with literal :

String str = “hello”;

Second is with “new” :

String str = new String(“hello”);

So what is the diffrence between both ?

If we are creating a string with literal , it goes to String Constant pool , And if we are creating string with “new”, then it goes to String heap.

String Constant pool is where string literals are stored, to avoid duplicate strings .

Because of String Constant pool, String is Immutable in Java.

Please note : String is not Primitive Type, Its a Object type in Java.

You can check my Android Apps here : BarCode Scanner

--

--

Jaspreet Singh
McAppMedia

Working in Android Developemen and Tech Mentor .My Github profile is: https://github.com/jaspreet1990 , Contact me at preetjaspreet90@gmail.com for more info.