File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes string function problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "string function problem" Watch "string function problem" New topic
Author

string function problem

priyanshu bhardwaj
Greenhorn

Joined: Jul 30, 2005
Posts: 20
How many objects of String are created?
string x="xyz";
x=x+"pqr";
tHANKS
Periakaruppan Thiagarajan
Ranch Hand

Joined: Jul 26, 2005
Posts: 65
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
priyanshu bhardwaj
Greenhorn

Joined: Jul 30, 2005
Posts: 20
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
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
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
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
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
My answer is 3
cathymala louis
Ranch Hand

Joined: Nov 02, 2005
Posts: 77
Hi

Three objects created.
ven kaar
Ranch Hand

Joined: Nov 01, 2005
Posts: 39
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


Parallax - Change in observational position that provides a new line of sight.
Simi gupta
Greenhorn

Joined: Oct 17, 2005
Posts: 7
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
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
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

    Joined: Jul 20, 2005
    Posts: 69
    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 ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: string function problem
     
    Similar Threads
    Request hint
    Vector and Array please help
    Question 9, Chapter 6 in K&B
    convert int ,double to string
    jQuery and Hidden Input type not working