Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String objects created

 
neha verma
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

As per my understanding 5 objects should be created-
"Fred"
"47"
"Fred47"
"ed4"
"ED4"
Please verify if I am correct.
Thanks.
 
Collins Mbianda
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi !!!

I would say that 4 objects are created:

"Fred", "Fred47", "ed4" and "ED4".

s = s + �47�; create on the heap "Fred47".
 
chandraiah chintakayalu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi neha,
according to me its only 3.
 
chandraiah chintakayalu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi neha,
chand here, according to me its only 3 objects are created when you invoke this method but in method many objects are created and lost.

correct me if im wrong.
 
neha verma
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi chand,
As per my understanding 5 objects are created and only one of the objects remains rest are all lost . So when the questions says how many objects are created when this method is invoked, which all objects are you counting. Can you please list them.
Thanks.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Collins well pointed out, only 4 String objects are created in code you posted.

  • Fred
  • Fred47
  • ed4
  • ED4



  • Now, the toString() method does not create a new object in the case of the String class, but returns the String itself.

    It is not this case, but the toUpperCase() method might also return the same object, instead of a new one, if all the characters of the String are already uppercase. Therefore...


    ...this would print true to the console because s and t are the same object.

    So, be careful. And as I said before, 4 objects are created as far as I can see.
    [ September 16, 2007: Message edited by: Edwin Dalorzo ]
     
    neha verma
    Ranch Hand
    Posts: 37
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,
    Can you please tell which all objects are lost?In such kind of question should we answer the total number of objects created or the objects created minus the objects lost.
    Another thing I guess you are not counting "47" as a String object.
    Please clarify.
     
    simmi kamal
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi neha,
    I agree with you there were a total of 5 objects created as follows:
    1. "Fred"
    2. "47" (lost)
    3. "Fred47" (Fred lost)
    4. "ed4" (Fred47 lost)
    5. "ED4" (ed4 lost)

    According to me since each time the reference variable s is assigned with a newly created String object the original String object it was referring to is lost.So, I think finally only 1 of the 5 objects is not
    lost in this process and the final result is "ED4".

    If the above code is modified as follows:

    Given:

    here too technically there are 5 objects created in the heap and 4 objects are lost but since the reference variable s is not reassigned with the values in lines 14 and 15 the final result would be "Fred47".

    although at this point even I am not sure what answer is expected whether 1. total no. of objects created or
    2. objects created - objects lost.

    Please correct me if I am wrong anywhere.
     
    Lucky J Verma
    Ranch Hand
    Posts: 278
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    so whats is the correct answer.
    As in refeence material ,answer sayd 3 objects.
    How do we count in exam
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic