0

String s=new String("somnath");

how many object will it create and why Or how?

20th Jun 2017, 3:13 PM
Somnath Ghosh
Somnath Ghosh - avatar
4 Réponses
+ 5
There comes a term 'string pool'. think of it as a notebook where string called are written. When we use double quotes to create a String, it first looks for String with same value in the String pool, if found it just returns the reference else it creates a new String in the pool and then returns the reference. However using new operator, we force String class to create a new String object in heap space. This means that the same thing will be written two times in the string pool or the virtual 'notebook'. This causes waste of memory and the first method is recommended.
20th Jun 2017, 3:23 PM
Snehit Sah
Snehit Sah - avatar
+ 2
I could not understand what you are trying to ask this time :-( please elaborate.
21st Jun 2017, 1:14 AM
Snehit Sah
Snehit Sah - avatar
0
for the first time? after that 1 object ?
20th Jun 2017, 4:33 PM
Somnath Ghosh
Somnath Ghosh - avatar
0
https://code.sololearn.com/cZzTT3H09obg/?ref=app I'm sure this code I've written will help u understand more clearly
23rd Jun 2017, 12:45 PM
Snehit Sah
Snehit Sah - avatar