• Post Reply Bookmark Topic Watch Topic
  • New Topic

String Doubt  RSS feed

 
Hardik Raja
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends,



As String are immutable, everytime we modify a String a new String is created..Considering the fact..can anyone tell

how many String objects will be created when go() method is invoked..

I guess...5 as follows..."HAR","89","HAR89","ar8","AR8"..
What do you think..?


Can anyone please give their suggestions on the above problem.Thank you in advance.

Regards,
Hardik.S.Raja

[ June 10, 2007: Message edited by: Hardik Raja ]

[ June 10, 2007: Message edited by: Hardik Raja ]
[ June 10, 2007: Message edited by: Hardik Raja ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot see where "har" comes from.
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strictly speaking, only 3 objects will be created when invoking the method.

It is true that there are 5 String objects involved: "HAR", "89", "HAR89", "AR8" and "ar8", but only three of them will be created during invokation of the go() method: "HAR89", "AR8" and "ar8".

The reason is that objects in the String literal pool ("HAR" and "89" in this case) are created when the class is loaded, so strictly speaking those String objects will already exist when you invoke go().
[ June 10, 2007: Message edited by: Sergio Tridente ]
 
Prasad Tamirisa
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, its totally 5 objects. Of them 3 got created with the invokation of the go() method.


I guess...5 as follows..."HAR","89","HAR89","har","AR8"..
What do you think..?


I too dont understand where had this "har" come from.
 
Hardik Raja
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya I made a mistake....there will be no "har"....there will be "ar8"..

I have edited my first message.....
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!