+ 1
What is the difference between these expressions
String a="hi"; String a = new String("hi"); What is the difference between these two ways of declaring 'a'
9 Réponses
+ 5
String a = hi;
⬆️ this is stored in string pool
String a = new String(hi);
⬆️ this is stored on heap it's not in string pool so when you compare both of these strings if the variables were diffrent using ==, the result returned would be false
+ 5
@D_Stark
So if you know that you will use a certain string very often you should initiate it as literal and if not you would initate it as object on the heap?
+ 4
Ayush Pandey the equals() function compares the strings and not the refreneces so it would return true as both strings are the same.
+ 1
Bro Airree i am not so begineer that i dont understand that much concepts.
+ 1
I wanna know what makes it different if we compare it.
+ 1
D_Stark what if we compare usinh equals to function will it output true
0
The first one declares a String, while the second one declares an object
0
It will be false, because there is an object, and objects are only equal if you assign it to be