• 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?

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,


I would like to know how many string objects created from the following first question. If your answer is two. Pls explain the second question. Because it says 8 objects or your answer is three why the K & B drawing show�s two objects (Chapter 6 and page no 6 of 54).


Question: - 1

String x = "Java";
x.concat(" Rules!");
System.out.println("x = " + x);

Question: - 2

String s1 = "spring ";
String s2 = s1 + "summer ";
s1.concat("fall ");
s2.concat(s1);
s1 += "winter ";
System.out.println(s1 + " " + s2);

Thanks, Raghu.K
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hum. I think there's some mistake here. Maybe someone else can explain better.

I think there are five and ten String objects for the questions #1 and #2, respectively, created and put in the String pool.

--------- F i r s t Q u e s t i o n ---------------

// Creates the first String object in the pool: "Java"
String x = "Java";

// Creates simultaneously two String objects :
//" Rules!" and "Java Rules!". The last created String object is not
// assigned to any variable but still exists in the pool
x.concat(" Rules!");

// At this point we have three objects in the String pool.
// So, with the last statement we are going to add more two String
// objects in the pool : "x = " and "x = Java"
System.out.println("x = " + x);

Total of String objects in the pool :
----------------------------------------
5 Objects : "Java", " Rules!", "Java Rules!", "x = " and "x = Java"


--------- S e c o n d Q u e s t i o n ---------------

// Creates the first String object in the pool : "spring "
String s1 = "spring ";

// Creates simultaneously two String objects :
// "summer " and "spring summer "
String s2 = s1 + "summer ";

// Creates simultaneously two String objects :
// "fall " and "spring fall "
s1.concat("fall ");

// Creates another one : "spring summer spring "
s2.concat(s1);

// Creates simultaneously two String objects :
// "winter " and "spring winter "
s1 += "winter ";

// At this point we have eight objects in the String pool.
// So, with the last statement we are going to add more two String
// objects in the pool : " " and "spring winter spring summer "

System.out.println(s1 + " " + s2);

Total of String objects in the pool :
----------------------------------------
10 Objects : "spring ", "summer ", "spring summer ", "fall ", "spring fall ", "winter ", "spring winter ", " " and "spring winter spring summer "


I'm really sure about the statements before the println call. I would like to hear your opinions about the println statements. I think they also create String objects in the pool.
[ April 18, 2006: Message edited by: Edisandro Bessa ]
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes ans is correct .8 objects are created.

String s1 = "spring "; //1 "spring " object is created.
String s2 = s1 + "summer ";// 2 objects are created in this line. "summer" and S1+summer="springsummer"
s1.concat("fall "); //2 objects in this line."fall" and "springfall"
s2.concat(s1); //1 object in this line. "springsummerspring"
s1 += "winter "; //2 objects in this line."winter" and "springwinter"
System.out.println(s1 + " " + s2);

i think you got the ans.
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In both questions. What about the println call ?

For example, in the first question we have.
System.out.println("x = " + x);

I think this call also creates more two String objects in the pool. : "x = " and "x = Java".
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are right Edisandro

5 string objects in Q1 and 10 string object in Q2
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the first question 3 string objects are created and in the second 8 are created.System.out.println() does not create any new string object.
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi kuturam,

I was really in doubt when I post my explanation here about the println call. That's why I asked your opinions.

Maybe you're correct or maybe not. Could you please put here some link or book reference where you saw such information that println doesn't create literal strings in String pool ?

Thanks in advance.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic