This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
How many objects of String are created? string x="xyz"; x=x+"pqr"; tHANKS
Periakaruppan Thiagarajan
Ranch Hand
Joined: Jul 26, 2005
Posts: 65
posted
0
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
SIR iCAN'T UNDERSTAND WHAT IS THE RIGHT ANSWER ONE OR TWO OR THREE? Thanks
vandu matcha
Ranch Hand
Joined: Dec 28, 2005
Posts: 57
posted
0
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
Joined: Dec 10, 2004
Posts: 6
posted
0
Hi,
Only 2 instances will be created since the Operator "New" isn't used while creating the string.
Cheers
Prashanth R<br />Chennai
sudhir harsha
Greenhorn
Joined: Jan 23, 2006
Posts: 19
posted
0
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
Joined: Jan 23, 2006
Posts: 19
posted
0
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
Joined: Oct 15, 2005
Posts: 163
posted
0
My answer is 3
cathymala louis
Ranch Hand
Joined: Nov 02, 2005
Posts: 77
posted
0
Hi
Three objects created.
ven kaar
Ranch Hand
Joined: Nov 01, 2005
Posts: 39
posted
0
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
Parallax - Change in observational position that provides a new line of sight.
Simi gupta
Greenhorn
Joined: Oct 17, 2005
Posts: 7
posted
0
Hi All, Please confirm the correct answer is two or three, i'm kind of cofused. Regards, Simi
steven gerrard
Ranch Hand
Joined: Jan 21, 2006
Posts: 55
posted
0
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
Joined: Nov 21, 2005
Posts: 91
posted
0
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
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.