This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Number of String objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Number of String objects" Watch "Number of String objects" New topic
Author

Number of String objects

Deepak Bajoria
Ranch Hand

Joined: Mar 07, 2008
Posts: 35
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


SCJP 5 - 93%
Sandeep Bhandari
Ranch Hand

Joined: Apr 16, 2004
Posts: 201
the only confusion was toString method.
referred the API and here's the toString method of String class



which makes your answer correct.


SCJP 96% | SCWCD 90%| SCJP mock exams | My SCJP blog
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14194
    
  20

This exact same question has been asked and answered many times before:

String Objects
How many string object created in this code
String Test
Strings
Q from javaBeat on String
Strings Doubt
Strings
doubt with string abjects
How many object create?
Doubt in creating strings...
How many String Objects are created

etc...


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Sazzad Hossain
Greenhorn

Joined: Jul 21, 2008
Posts: 10
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

Joined: Nov 05, 2007
Posts: 661
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 ]

The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
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

Joined: Jul 05, 2008
Posts: 141
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.


SCJP Java 6 (98%) - Story, SCJA (88%) - Story
 
wood burning stoves
 
subject: Number of String objects