File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Number of String objects

 
Deepak Bajoria
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many String objects will be created when this method is invoked?



As per my understanding string object created would be:
"Fred",
"47",
"Fred47"
"ed4"
"ED4"

Please correct me if wrong
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the only confusion was toString method.
referred the API and here's the toString method of String class



which makes your answer correct.
 
Sazzad Hossain
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Deepak

From my understanding you are right. total 5 objects will be created. the toString() method will return the object itself. It will not creat any new object according to sun api.
 
Nabila Mohammad
Ranch Hand
Posts: 661
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Deepak Bajoria:
How many String objects will be created when this method is invoked?



As per my understanding string object created would be:
"Fred",
"47",
"Fred47"
"ed4"
"ED4"

Please correct me if wrong


Sandeep,
Are you completely sure?

Because ther has been a lot of discussion on this question (That's another thing that I still don't remember the correct answer!)

But as far as i remember..
I remember some one mentioning that this question asks "when this method is invoked"
And "47" is created in the pool during compile time, so in that case "47" won't be included in the list of String created.

I belive there are 4 Strings that are created.

Some one correct if I am wrong.
[ July 21, 2008: Message edited by: Nabila Mohammad ]
 
Nabila Mohammad
Ranch Hand
Posts: 661
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like there are 3 Strings created when the method is invoked.

"Fred" and "47" are String literals which are created when the class is loaded.

toString() doesnot create a new String.


Thanks Jesper for the link.
 
Raphael Rabadan
Ranch Hand
Posts: 141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nabila Mohammad:
Looks like there are 3 Strings created when the method is invoked.

"Fred" and "47" are String literals which are created when the class is loaded.

toString() doesnot create a new String.


Thanks Jesper for the link.


I've made the same question before. And yes, it's 3 because "Fred" and "47" are already created.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic