This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

String Manipulaton  RSS feed

 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have attended one interview and there was a question on Strings...

how many objects are created in the following code?



i know that Strings are immutable and it refers to the objects in the pool. If new keyword is used, then it will create a new object and will not refere to the existing object in the pool

according to my knowledge, there will be 2 objects created. Let me know if i am going wrong somewhere.

Regards,
Anu
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In all those three lines together, three String objects will be created:

  • One for the string literal "Hello" (line 1), which is put in the literal pool
  • One explicitly created String object (line 1), which copies its content from the first String object
  • One for the string literal "hello" (lower-case h, lines 2 and 3)


  • Note that in line 3, the compiler is smart enough to do the concatenation of "hel" and "lo" at compile time, so the effect is exactly the same as line 2.
     
    Vijitha Kumara
    Saloon Keeper
    Posts: 3997
    42
    Chrome Fedora Hibernate
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jesper Young wrote:
  • One for the string literal "Hello" (line 1), which is put in the literal pool

  • Hi Jasper,
    Will a reference be added to the SCP at the invocation new String("Hello") ?

     
    Rahul P Kumar
    Ranch Hand
    Posts: 188
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    there will be 5 objects. in addition to those mentioned by Jesper, there will be 'hel' and 'lo' , which also goes into string pool
     
    Anu satya
    Ranch Hand
    Posts: 146
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    so,
    in line 3, hel and lo is concatenated at the compilation time.
    after compilation, at the run time, pointer of s3 will be pointing to s2 right? or is it a new string created in the pool?
     
    Campbell Ritchie
    Marshal
    Posts: 55681
    162
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    "hel" + "lo" makes three Strings.
  • 1: "hel"
  • 2: "lo"
  • 3: "hello" which reuses the identical String from the previous line
  • So you are correct.

    If you went to an interview and didn't know to tell them that the Strings are compile-time constants, you will not have done very well.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!