aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How many String Objects are created by executing the following method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How many String Objects are created by executing the following method" Watch "How many String Objects are created by executing the following method" New topic
Author

How many String Objects are created by executing the following method

SenthilKumar Ponnuswamy
Greenhorn

Joined: Nov 25, 2008
Posts: 2
Public String makingStrings() {

String s = “Fred”;

s = s + “47”;

s = s.subString(2,5);

s = s.toUpperCase();

return s.toString();
}

How many String Objects will be created when this method is invoked?

A. 1

B. 2

C. 3

D. 4

E. 5

F. 6
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Please quote where that question comes from, and please tell us what you think the answer is.

By the way: a search will probably reveal similar threads; that same question appears here quite frequently.
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
i think answer will be 4. other than s.toString() all other lines will generate String object


<a href="http://technologiquepanorama.wordpress.com" target="_blank" rel="nofollow">My Techie Blog</a><br /><a href="http://www.java-questions.com" target="_blank" rel="nofollow">Java Questions</a>
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Harshit Rastogi wrote:i think answer will be 4. other than s.toString() all other lines will generate String object

But the second line creates two strings - the literal "47" and the concatenation of s and "47".


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3168
    
  10
I think the answer depends on when String literals get created. Without checking the Java Language Specification to be sure, I believe String literals are created when the class is loaded. If this is right, then the answer is 3.

If however, String literals are created when a line containing that literal is executed, then the answer cannot be determined. All String literals are pooled, therefore if another piece of code containing "Fred" and "47" has already been executed, then these String literals will already exist and will not be recreated by this code. Therefore the answer would be 3. If however, neither of the literals had already been created, then the answer would be 5. And if only one of the literals had already been created the answer would be 4.

Assuming this is not a trick question and that the options of 'the answer cannot be determined' is not available, leads me to believe that my vague memory that String literals are created when the class is loaded is correct. So the answer is 3.


Joanne
Senthil Kumaran
Ranch Hand

Joined: Aug 24, 2008
Posts: 32
by the way whats the correct answer, as by question number of created strings, would be 4 right, adding tostring method then its five

1.fred,
2.fred47,
3.ed4,
4.FRED,
5.fred.(tostring).
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3168
    
  10
Senthil Kumaran wrote:adding tostring method then its five


No. Take a look at the source code for the String.toString() method. You can find it in the src.zip file in your JDK installation.

And your value for 4 (FRED) is incorrect.
Senthil Kumaran
Ranch Hand

Joined: Aug 24, 2008
Posts: 32
the number of string objects created are 4, am sure,
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

The SCJP answer is 5: two literals, the concatenation, and the calls to substring and toUpperCase. toString doesn't return a new String.

But outside of the SCJP exam, these kinds of questions are totally meaningless; in no sense does any "creating" of the two String literals happen while this code is executing. So the "real" answer is 3.

But as I suspect we're talking SCJP here, go with 5, and I'll move this to the SCJP forum, to boot.


[Jess in Action][AskingGoodQuestions]
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
The simple rule is Strings in java are immutable, that is they can never change. Even if it looks like you are changing a string you are not you are creating a new string, and / or moving the reference variable to the new string. If the String is a literal and the JVM finds a match in the pool then the reference simply points to the existing literal.



so 4 new objects created here! Hope this helps


be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Suresh K Babu
Greenhorn

Joined: Feb 10, 2007
Posts: 16
4 string objects are created...

1. Fred
2. Fred14
3. ed1
4. ED1
Raf Szczypiorski
Ranch Hand

Joined: Aug 21, 2008
Posts: 383
Ernest is right, it is 5:
2 literals: "Fred" and "47"
then 3 others: Fred47, ed4, ED4.
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
How so 5?



I make that 4. The toString() method does not create a new String Object
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

Ah, the famous "Fred 47" question again.

This question has been asked and discussed numerous times before here on JavaRanch. Please do a search, and search for "Fred 47" - you'll find a lot of discussions about this question.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Raf Szczypiorski
Ranch Hand

Joined: Aug 21, 2008
Posts: 383
"47" is used in concatenation, that's right. But, it is also used as a literal, so it is put into the pool internally. So, "Fred" and "47" are in the pool, and "Fred47" is the third one. Then there is the substring and toUpperCase.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How many String Objects are created by executing the following method
 
Similar Threads
String Objects...
How many String objects will be created?
Strings
how many String objects created??
please provide the explanation regarding string objects............