• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

K&B pg 359: No of string objects?

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the K&B book, in the following example, 2 reference variables and 8 String objects are created.
String s1 = "spring ";
String s2 = s1 + "summer ";
s1.concat("fall ");
s2.concat(s1);
s1 += "winter ";
System.out.println(s1 + " " + s2);
I thought 9 objects should be created. Isnt " " also created as a String object? Please comment.
Thanks,
Cathy.
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would also say 9.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cathy !
Okay let's exmaine all references and objects created one-by-one
according to the following code :
1.String s1 = "spring ";
2.String s2 = s1 + "summer ";
3.s1.concat("fall ");
4.s2.concat(s1);
5.s1 += "winter ";
6.System.out.println(s1 + " " + s2);
---------------------------------------------------------------
Line#Object(s) Reference(s)
1springs1
2summer,springsummers2
3fall"Temporary"
4--------
5winter,springwinters1
6springwinter springsummer," " "Temporary"
---------------------------------------------------------------
it's now clear that there are only 8 objects and 2 references,Hope it's
now clear.
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mostafa, I have to disagree with you on that. I too think there are 9 objects as such:

Line 1: String "spring " is created. So far 1 String object.
Line 2: Strings "summer " and "spring summer " are created. So far 3 String objects.
Line 3: String "fall " is concatenated to string s1, "spring ", but the resulting new string isn't assigned to any variable. So far 5 String objects.
Line 4: String s1 is concatenated to string s2 but the resulting new string isn't assigned to any variable. So far 6 String objects.
Line 5: String "winter " is concatenated to string s1 and resulting new string assigned to variable s1. So far 7 String objects.
Line 6: Strings s1, " " and s2 are concatenated and the result printed out. The 'empty' string is String object nr. 8 and the result of concatenation String nr. 9.
So, 9 String objects and 2 references.
[ September 23, 2003: Message edited by: Mika Leino ]
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can see even... 11 String objects:
1. "spring " is created and the reference assigned to s1
2. "summer " is created and abandoned
3. "spring summer " is created and referenced by s2
4, "fall " is created and abandoned
5. "spring fall " is created and abandoned
6. "spring summer spring " is created and abandoned
7. "winter " is created and abandoned
8. "spring winter " is created and referenced by s1
9. " " is created
10. "spring winter " is created
11. "spring winter spring summer " is created and printed out
 
Mika Leino
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read my earlier posting again and went through that piece of code again. I still think that there are 9 objects but I thought I'd better rewrite my earlier post.

Line 1: String "spring " is created. So far 1 String object.
Line 2: Strings "summer " and "spring summer " are created. So far 3 String objects.
Line 3: String "fall " is concatenated to string s1, "spring ", but the resulting new string isn't assigned to any variable. So far 5 String objects.
Line 4: String s1 is concatenated to string s2 but the resulting new string isn't assigned to any variable. So far 6 String objects.
Line 5: String "winter " is concatenated to string s1 and resulting new string assigned to variable s1. So far 8 String objects.
Line 6: Strings s1, " " and s2 are printed out. The 'empty' string is String object nr. 9.
I think I'll better read all questions in the exam twice
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, let's not forget that + operator is overloaded for String concatenation, and String concatenation operation, in turn, creates at least one new object because Strings are immutable. So, System.out.println(s1+" "+s2); should create 3 new objects in case " " object is not in the String pool yet. What do you think?
Of course, the question says "prior to the println() statement", so there are 9 objects indeed!!!
[ September 23, 2003: Message edited by: Vad Fogel ]
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm...I'm getting 10. By line number:
1. "spring "
2. "summer ", "spring summer "
3. "fall ", "spring fall "
4. "spring summer spring "
5. "winter ", "spring winter"
6. " ", "spring winter spring summer "
Concerning line 6, the compiler will probably use a StringBuffer to concatenate the three strings, so there's no reason to count an additional temporary string (as if it were going to concatenate twice).
By the way, best not refer to " " as the 'empty' string - it is not empty. This term belongs to the string "", which contains no characters.
 
Mika Leino
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used that 'empty' because I couldn't come up with a better word at the time. If I were to write my post right now I'd probably use whitespace string
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's really confusing -

The Java language provides special support for the string concatentation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuffer class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java. For additional information on string concatenation and conversion, see Gosling, Joy, and Steele, The Java Language Specification.


Kathy says there are 8 objects being created prior to the println() statement. Go figure now where concatenation doesn't result in a new object.
Java 2 Platform
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me show you how I came up with 9 string objects:
String s1 = "spring "; // "spring" (1)
String s2 = s1 + "summer "; // "summer" (2),"springsummer " (3)
s1.concat("fall "); "fall "(4), "springfall "(5)
s2.concat(s1); // "springsummerspring" (6)
s1 += "winter "; // "winter " (7), "springwinter " (8)
System.out.println(s1 + " " + s2); // " " (9)
*** println uses a stingbuffer (not string) to create its argumrent to print.
 
author
Posts: 9031
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So did we survive?
Given that you're supposed to ignore the SOP, what's the concensus?
-Bert
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is SOP (Standard operating procedure)
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Barkat, it'd be awesome if you provided a link to println() or print() methods using StringBuffer instead of String for concatenation. I haven't found it so far in JLS.
[ September 23, 2003: Message edited by: Vad Fogel ]
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

what is SOP (Standard operating procedure)


Bert was referring to the System.out.println when he said SOP
So if you ignore that line you get 8 objects as described in some of the earlier posts as well as Bert and Kathy's book
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Who can explain when and why a new String object is not created as a result of concatenation? I've just read that it's sometimes up to the compiler to optimize the code and use StringBuffer's append() method for concatenation.
 
Steve Lovelace
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aaah hah hah! So minus the 1 (according to Barkat) or 2 (according to me) strings created in the 'SOP', the answer is 8. Kathy and Bert are spot on (anyone surprised?).
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[q]
Barkat, it'd be awesome if you provided a link to println() or print() methods using StringBuffer instead of String for concatenation. I haven't found it so far in JLS.[/q]
Hi Vad,
I read this in one of the other thread in this forum.
Thanks
Barkat
 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I better start reading questions atleast twice before answering them. I totally missed the part about *prior* to println
Thanks for all your help.
--Cathy.
 
Ranch Hand
Posts: 2378
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are 11 string objects here including the last sop statement. As the question asked for the number of string objects created prior to that sop statement, the number is 11 - 3 = 8
I think Kathy & Berty survived this time.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Steve 8 string objects prior to SOP and 10 after it.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree with Steve: 8 before pint and 10 after. :roll:
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,
I am a bit confused.... string literals are interned.. and all string literals are maintained in string pool and not created for every instance and it happens at class load time and coming to SOP any object gets printed using objects toString() method there is no way u can print an object directly. One has to override toString() method to print.

So to my knowledge only 5 objects are been created in the process.
- Srinivas.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My initial thoughts: there are 8
Run it through a profiler: there are still 8
Conclusion: there are 8
 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tony,
This is probably a stupid question, and I guess people know
how to do this..but can you please tell me how to use the profiler?
If you could direct me to any references I would really
appreciate it.
Thanks,
Cathy.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.jprofiler.com
I think it's also important to note what goes on internally in java.lang.String#concat(String). i.e. A String instance is created and returned, however, the return value is ignored and therefore, is not counted in the total count of String instances.
 
Do Re Mi Fa So La Tiny Ad
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic