Difference between String s = "abc"; and String s = new String("abc"); | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Difference between String s = "abc"; and String s = new String("abc");

String class

16th Sep 2018, 4:55 AM
Shubham Mhatre
Shubham Mhatre - avatar
4 Answers
+ 8
String s = new String("abc"); - creates new object. String s = "abc"; - replaces existing object.
16th Sep 2018, 5:48 AM
Email Not Activated
+ 8
you should also take a look at were these strings are stored in string pool and heap memory and the diffrence between them both when creating objects
16th Sep 2018, 8:26 AM
D_Stark
D_Stark - avatar
+ 5
It is really a good concept. When you write String s=new String("abc"); it will create a new object every time having different reference(memory location) but when you write String s="abc"; it will point to already existing object in memory pool means reference is same. https://stackoverflow.com/questions/3052442/what-is-the-difference-between-text-and-new-stringtext
16th Sep 2018, 5:43 AM
Alpha Rays
Alpha Rays - avatar
+ 3
String class are store in heap and it copies one string to scp(string constant pool) and if one string has the same data then String="abc" wont create it replicate other
20th Sep 2018, 5:02 PM
Aarav Raj
Aarav Raj - avatar