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 Beginning Java and the fly likes How many String Object are created in the following code ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How many String Object are created in the following code ??" Watch "How many String Object are created in the following code ??" New topic
Author

How many String Object are created in the following code ??

Rohan Kayan
Ranch Hand

Joined: Sep 17, 2004
Posts: 123
String s1 = "spring ";
String s2 = s1 + "summer ";
s1.concat("fall ");
s2.concat(s1);
s1 += "winter ";
System.out.println(s1 + " " + s2);


SCWCD 1.4, SCJP 1.4
Sandeep Shukla
Greenhorn

Joined: Oct 21, 2004
Posts: 5
String s1 = "spring "; # 1
String s2 = s1 + "summer ";#2
s1.concat("fall "); #3
s2.concat(s1);#4
s1 += "winter ";#5
System.out.println(s1 + " " + s2); #6

//net 6 ....(as + and concatenation operations are stringbuffer operations and the final thing is converted to string)
Eric Zanders
Greenhorn

Joined: Sep 15, 2004
Posts: 16
Rohan,

I would say 5:
1� s1.
2� s2.
3� The string concatenation is not stored in s1 but it exists.
4� Same case for the s2 string concatenation.
5� The string you'll print is the concatenation s1, a blank and s2.

To verify this I created a little testprogram:

When you run this, you'll notice that:
- s1 only gets concatenated with "winter " because of the statement
s1 += "winter "; which is written out in full s1 = s1 + "winter ";
thus resulting in modification of s1.
- s2 is never modified.

Greetz

J
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


thus resulting in modification of s1.

Strings are immutable. You cannot modify a String object.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Eric Zanders
Greenhorn

Joined: Sep 15, 2004
Posts: 16
Dear Paul,

As I'm only a beginner I tend to make mistakes. (which is normal, I guess...)

Would it have been more correct if I had stated that the content of the object s1 was modified?

Anyway, thanks for correcting me. How else am I going to learn from my mistakes?

Greetz

J
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Mistakes are expected - we all make them.


Would it have been more correct if I had stated that the content of the object s1 was modified?

No. Think of "immutable" as meaning "read-only". If I write this:

What I've got is one reference (s1) which points to a String object. In line 1 the reference points the the value "initial string". In line two the value of the object doesn't change (becasue it can't), instead another String object is created with the value of "initial string more characters" and the reference s1 is pointed at that. The original String object still exists, its just no references point to it anymore.
Eric Zanders
Greenhorn

Joined: Sep 15, 2004
Posts: 16
Thanks Paul.
So returning to the initial question, the result (number of String objects) then should be 6?
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I count at least 9:

Remember that String objects are created for each of the literals in the code.

Layne


Java API Documentation
The Java Tutorial
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
javanic1,

Welcome to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

Thanks Pardner! Hope to see you 'round the Ranch!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Rohan Kayan
Ranch Hand

Joined: Sep 17, 2004
Posts: 123

Remember that String objects are created for each of the literals in the code.

Layne[/qb]<hr></blockquote>
You are right that each of the literals in the code will be a string object but from that point of view the count should be 10 as in the last line , when s1 + " " it will create a new string object with s1 and blank in the last. Then again this object will be again concatnated with s2 creating another string object .

[ October 24, 2004: Message edited by: Rohan Kayan ]
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

I would recommend to read this article about Strings on javaranch itself,
this is very nice article and definitely you will get answer to your question.


Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
 
GeeCON Prague 2014
 
subject: How many String Object are created in the following code ??