• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul.
So returning to the initial question, the result (number of String objects) then should be 6?
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Happily living in the valley of the dried frogs with a few tiny ads.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic