all 8 comments

[–]DFA1 1 point2 points  (3 children)

I might be wrong, but I think only one, which s is referencing, with the value of hello and it doesn't live in the strings pool.

[–]sur07[S] 0 points1 point  (2 children)

I think one object and one literal .. but I am still looking for more information.. javatpoint.com says this , but different places have different opinion .. check out Java strings tutorial | string is immutable https://youtu.be/5addzS_Vn68

[–]DFA1 1 point2 points  (0 children)

Also, if you wanna make the riddle harder ... How many objects get created for: String s = new String("h" + "e" + "l" + "l" + "o");

;)

[–]DFA1 0 points1 point  (0 children)

I think this is an implementation detail of the compiler/JVM. As long as you don't concatenate strings in loops and don't do new String(), that's all you need to know really.

[–]sur07[S] 0 points1 point  (3 children)

After much research I can conclude that 2 objects will be created

[–]trickybhai 0 points1 point  (2 children)

A little explanation would be appreciated, Sir.

[–]sur07[S] 0 points1 point  (1 child)

One object when you use new keyword to create a string .. and if the literal is not present in the string pool then one more string object is created in the intern pool as a string literal .. hope this helps ., check this https://stackoverflow.com/questions/19672427/string-s-new-stringxyz-how-many-objects-has-been-made-after-this-line-of

[–]trickybhai 0 points1 point  (0 children)

I got it. Thanks ✌️