• 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

String objects

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The below code creates 4 String objects


Correct me if am wrong.
Thanks,
Ramnath

Feed an Opportunity.Starve a Problem

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
posted by ramnath
------------------------
String s1 = �Hello�;--(1)
String s2 = s1;--(2)
s1 = s1.concat(�World�);--(3)
s2.concat(�!!!�);--(4)
-----------------------------------
u r wrong dear :
here only three string object are created
At line 1
at line 3
at line 4
line 2 s2 pointed the same objected created at line i
bcoz this strings object r created in literial pool
if the same string availlabe in the pool then Jmn not going to created new string object
ok
whatever i know i have written
it may be this is usefull for u
sameer kumar
 
Ramnath krishnamurthi
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sameer,
Yup.You are right.s2 simply points to s1 and no new String object is getting created because of the assignment.
Thanks,
Ramnath

Feed an Opportunity.Startve a problem

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys!
The question was: how many String objects were created alltogether and I certainly disagree with sameer's 3.
Because...
String s1 = �Hello�; //creates 1st one
String s2 = s1; // no new objects
s1 = s1.concat(�World�);
//create "World" and "HelloWorld" objects. "World"
//is lost but s1 is referring now to "HelloWorld"
//so 2nd and 3rd objects are created
s2.concat(�!!!�); //4th object's created but it lost at once
So 4 objects alltogether is how I undertand it?
 
chris anderson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry!
5 objects
if we are to follow the logic
explained in the previous reply!!!
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about this one ?
String x=new String("abc")
How many objects will be created in the above statement..I thought it will create only one object..but K&B says 2 ..
please explain
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer to the original queston is 5. Each of the strnig literals causes a String object to be created and the two invocations of concat create new String objects.
To answer Karthik's question, when you have a line of code like this:

You are really creating 2 String objects. First of all, the String literal, "test," causes a String object to be created when the class is loaded (the String literal is treated as a constant). Then, when we invoke the constructor of the String class and pass it that literal, we are creating a second String object.
So what's the best news in all of this? None of this is on the exam.
 
reply
    Bookmark Topic Watch Topic
  • New Topic