Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

string function problem

 
priyanshu bhardwaj
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many objects of String are created?
string x="xyz";
x=x+"pqr";
tHANKS
 
Periakaruppan Thiagarajan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by priyanshu bhardwaj:
How many objects of String are created?
string x="xyz";
x=x+"pqr";
tHANKS


In this case, totally three strings("xyz", "pqr", "xyzpqr") will be created in the constant pool. To see more on this, click the following link
String constant pool
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Periakaruppan Thiagarajan:
in the constant pool.


No, actually -- only the two literal (quoted) strings are in the constant pool. The concatenation is done at runtime.

I'm going to move this to SCJP, just to make the point that these sorts of questions belong there, not here.
 
priyanshu bhardwaj
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SIR
iCAN'T UNDERSTAND WHAT IS THE RIGHT ANSWER ONE OR TWO OR THREE?
Thanks
 
vandu matcha
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
according to my understanding when u create String x="xyz"..then any matches to this literal "xyz" in the String pool is found out..if any match is found...then it is reffered by variable x...therefore here new object is not created....when we performed the concatenation in the later step....new object is created and is reffered back to variable x....hence only one object is created...this is only a guess...waiting for the correct answer...... wats the ans...
 
Prashanth Raghavan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Only 2 instances will be created since the Operator "New" isn't used while creating the string.

Cheers
 
sudhir harsha
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many objects of String are created?
string x="xyz";
x=x+"pqr";
tHANKS


Since Strings are immutable , Of total three Obj's are created.
1 -> "xyz" ; even though it exists in memory, it is considered lost after reassigning to 'x'.
2->
 
sudhir harsha
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many objects of String are created?
string x="xyz";
x=x+"pqr";
tHANKS


Since Strings are immutable , Of total three Obj's are created.
1 -> "xyz" ; even though it exists in memory, it is considered lost after reassigning to 'x'.
2-> "xyzpqr"
3-> "pqr" itself is one object.
Plz suggest, if I'm wrong.

regards,
Sudhir
 
Naresh Gunda
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My answer is 3
 
cathymala louis
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Three objects created.
 
ven kaar
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I go with Ernest, only 2 objects are created,

String literals are created at compile time and not at runtime

string x="xyz";
x=x+"pqr";

only "xyz" and "pqr" are created and not "xyzpqr"

there was a previous discussion already in this forum, plse find the link below

http://www.coderanch.com/t/246349/java-programmer-SCJP/certification/Journal-Article-SCJP-Tip-Line
 
Simi gupta
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Please confirm the correct answer is two or three, i'm kind of cofused.
Regards,
Simi
 
steven gerrard
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think 3 objects will be created 2 in pool and one not in pool
x="xyz"; creates a string object "xyz" in pool
x=x+"pqr";it creates a string object "pqr" in pool and since value of variable x is evaluated at runtime the new string object "xyzpqr" will be created outside of pool
 
Tilo Hemp
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this question was discussed just a few days ago, after many thoughts i am pretty sure that three objects are created.

my references are these three:
  • http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html, where the situation String s = new ("someString"); is explained explicitly
  • the java language specification. quote: "Each string literal is a reference to an instance of class String",
  • K & B book page 360


  • anyway, if somebody has a strong counter-argument, then i am able to resign from this position

    greetings
     
    Lalitha Vydyula
    Ranch Hand
    Posts: 69
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi All,

    The below example from K&B should help.

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

    The result of this code fragment is �spring winter spring summer�.
    There are two reference variables, s1 and s2. There were a total of eight String objects created as follows: �spring�, �summer � (lost), �spring summer�, �fall� (lost), �spring fall� (lost), �spring summer spring� (lost), �winter� (lost), �spring winter� (at this point �spring� is lost). Only two of the eight String objects are not lost in this process.

    See String Objects

    [ January 30, 2006: Message edited by: Lalitha Vydyula ]
    [ January 30, 2006: Message edited by: Lalitha Vydyula ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic