• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

How many String objects are created ?

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all
This question was allready posted on this forum..but i could not find the satisfactory answer.
so again posting it....
String s1 = "java";String s2 = "exam";System.out.println(s1 + " " + s2);
How many String objects are created ?
Thanks in advance
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Karan
My guess is 4.
1. java
2. exam
3. (space)
4. java exam
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Karan Gulati:
String s1 = "java";String s2 = "exam";System.out.println(s1 + " " + s2);
How many String objects are created ?


I'm not 100% on this, but I think the answer is actually 5. Obviously, "java", "exam", and (space) Strings are created - they're String literals.
However, when you evaluate the expression in the println statement, you don't evaluate it all at once. Rather, you first evaluate s1 + " " to get the String "java " (so that's the 4th String) and then you perform the second concatenation to get "java exam" (which is the 5th String).
If I'm mistaken, someone please correct me, but I believe the correct answer is 5.
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that there are 4 String objects:
1) There are 3 objects from the constant pool. (see lines 0, 3, & 20)
2) 1 temporary String object (line 29)
Line 9 also shows that a StringBuffer is used to temporary contain the strings which is later converted to a String object using toString() method.

At least this is how my compiler does it.
[ August 14, 2003: Message edited by: Alton Hernandez ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alton brings up an excellent point. We don't REALLY know how many Strings are going to be created because the compiler may provide some optimizations, as was the case in his response.
However, someone else's compiler might use my solution, which isn't as efficient.
Regardless, don't expect to see this on the exam.
 
Munish Gulati
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everybody
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI all,
According to me, only 3 String objects will be created..
The concatenation is done using StringBuffer object.. so
s1 + " " + s2 is not represented as String object rather it is a string BUffer object and then finally we have a String object.. All the concatenation operation done by compiler, uses StringBuffer.
Please correct me, if i am wrong.
Regards,
Harry
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. So what's the right answer? 3/4. As Harry said that concatenation is done using string buffer and not the string objects and that's what the jdk1.4 api also says. So is the right answer 3?
Thanks
Deep
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Posted by Corey:

Regardless, don't expect to see this on the exam.


Are you sure Corey? I found following K & B practice test:

3. Given the following,
13. String x = new String("xyz");
14. y = "abc";
15. x = x + y;
how many String objects have been created?
A. 2
B. 3
C. 4
D. 5

 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The aswer is four because we can see append three times invoked each with a string argument.
Why " " was going to be discriminated respect others string literals?
 
reply
    Bookmark Topic Watch Topic
  • New Topic