• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How many String Objects are created by executing the following method

 
SenthilKumar Ponnuswamy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50168
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think answer will be 4. other than s.toString() all other lines will generate String object
 
Rob Spoor
Sheriff
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Senthil Kumaran
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the number of string objects created are 4, am sure,
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Suresh K Babu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
4 string objects are created...

1. Fred
2. Fred14
3. ed1
4. ED1
 
Raf Szczypiorski
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest is right, it is 5:
2 literals: "Fred" and "47"
then 3 others: Fred47, ed4, ED4.
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How so 5?



I make that 4. The toString() method does not create a new String Object
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raf Szczypiorski
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic