This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Rohan Kayan
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String s1 = "spring ";
String s2 = s1 + "summer ";
s1.concat("fall ");
s2.concat(s1);
s1 += "winter ";
System.out.println(s1 + " " + s2);
 
Sandeep Shukla
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

thus resulting in modification of s1.

Strings are immutable. You cannot modify a String object.
 
Eric Zanders
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul.
So returning to the initial question, the result (number of String objects) then should be 6?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I count at least 9:

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

Layne
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Rohan Kayan
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1082
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic