• 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

One more question on strings.

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is an empty string considered as a new String object ?
e.g. tell how many new string objects are created ?
String s="abc";
String b="kau";
System.out.println(s+" "+b);
so, the ans should be 2 or 3 ?
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Strings with only whitespace in them do count as String objects. So the answer is 3.
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The total number of String objects are 4 these are s, b, " " and then the final concatenated String object..
Please correct me if i am wrong!!!
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a note on terminology: an "empty String" contains no characters. You can make one like this: s = "", or like this: s = new String(). The one used above is not empty.
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Finally what is the answer???
Is it 3 or 4 or 5.
I think it is 5...because in string conctatenations like
s+" "+b
we also have the string object "abc " formed.
So I think there are 5
"abc"
"kau"
" "
"abc "
"abc kau"
Please let me know if I am correct.
Thanks
Lalitha
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only 4 are created. A StringBuffer is created, the three strings are appended to it. Then StringBuffer.toString is invoked returning the resulting string.
Because this process might depend on the JVM, these kind of questions are not likely to appear in the exam.
 
Lalitha Chandran
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jose...
 
reply
    Bookmark Topic Watch Topic
  • New Topic