[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Win a copy of JBoss AS 5 Development this week in the JBoss forum
or Spring Dynamic Modules in Action in the Spring forum!
Reply Bookmark it! Watch this topic JavaRanch » Forums » Professional Certification » Programmer Certification (SCJP)
 
RSS feed
 
New topic
Author

How many String Objects are created

sakthi moorthy
Greenhorn

Joined: Oct 17, 2007
Messages: 25

11. public String makinStrings( ) {
12. String s = "Fred"; // this line creates one object
13. s = s + "47"; // this line creates two objects
14. s = s.substring( 2, 5); // this line creates one object
15. s = s.toUpperCase( ); // this line creates one object
16. return s.toString() ;
17. }

how many String Objects are Created in that code thanks in advance
lunatic chan
Greenhorn

Joined: Nov 08, 2007
Messages: 2

8 , i suppose
aniruddha gaikwad
Greenhorn

Joined: Nov 01, 2007
Messages: 14

5
Jesper Young
Java Cowboy
Bartender

Joined: Aug 16, 2005
Messages: 7631

aniruddha, please check your private messages. You can see them by clicking My Private Messages.

Java Beginners FAQ - JavaRanch SCJP FAQ
The Java Tutorial - Java SE 6.0 API documentation
Marinho Tobolla
Greenhorn

Joined: Mar 31, 2005
Messages: 3

I woud guess :

12 -> 1 String "Fred"
13 -> 2 Strings "47" and "Fred47"
14 -> 1 String "ed47"
15 -> 1 String "ED47"
16 -> s.toString creates a String "ED47"

makes a sum of 6.
Kelvin Lim
Ranch Hand

Joined: Oct 20, 2007
Messages: 512

I'm pretty sure the answer is 3.

SCJP 5.0
Jim Yingst
wanderer
Sheriff

Joined: Jan 30, 2000
Messages: 18569

Unfortunately this isn't a very good test question, and it's not representative of the questions you'll get on the real exam. The problem is that the compile-time constants "Fred" and "47" have special behavior that's more subtle then you need to know for the exam, and it's ambiguous to say that they were "created" in the code. What happens is, String objects for "Fred" and "47" are created when the class is loaded, not when the method is run. And if the method is run more than once, "Fred" and "47" aren't recreated - they just get re-used. So, if you run the method just once and exit, you get:

"Fred", "47" created when class is loaded.
"Fred47", "ed47", "ED47" created when the method is run.

That's 5 strings total.

If you run the method 5 times, you get

"Fred", "47" created when class is loaded.
"Fred47", "ed47", "ED47" created when the method is run the 1st time
"Fred47", "ed47", "ED47" created when the method is run the 2nd time
"Fred47", "ed47", "ED47" created when the method is run the 3rd time
"Fred47", "ed47", "ED47" created when the method is run the 4th time
"Fred47", "ed47", "ED47" created when the method is run the 5th time

That's 17 strings over 5 method calls.

Additionally, in the event that some other code has used the constants "Fred" or "47" before the method is called, those strings won't be recreated at all. So it's possible that the above code could create just 3 objects when run once, and 15 object when run 5 times.

Based on all this, you can guarantee that at least three String objects are created when the method is run, and at most, five. I would say that three String objects are created in the method, and two may be caused to be created when the class is loaded.

Ultimately I don't think there's any way to determine what the "best" answer is for this question because, the way it's phrased, it's ambiguous. I wouldn't worry too much about it. The real exam won't have a question like this anyway.

"I'm not back." - Bill Harding, Twister
Jim Yingst
wanderer
Sheriff

Joined: Jan 30, 2000
Messages: 18569

[Marinho]: 16 -> s.toString creates a String "ED47"

It turns out that no new String is created in that line. If you check the API for toString() in the String class, it says that the method just returns the same object you started with. There's no need to create a new String here, and it doesn't.

However, I'm pretty sure this particular detail is not on the exam either; it's too subtle. If you look through all the methods of String, some of them say they always create a new String, period. Others say they will not create a new String if the result is the same as the string they started from. And others do not say one way or another (so we really don't know). It would be unreasonable to expect programmers to memorize all the variations of behavior which are possible here, and so the exam does not require this level of detail. So once again, this is something that you shouldn't worry too much about.

"I'm not back." - Bill Harding, Twister
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Messages: 3028

Both of your explanations were too good Jim

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials]
 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Professional Certification » Programmer Certification (SCJP)
 
RSS feed
 
New topic
MyEclipse Enterprise Workbench