• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

string function problem

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How many objects of String are created?
string x="xyz";
x=x+"pqr";
tHANKS
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SIR
iCAN'T UNDERSTAND WHAT IS THE RIGHT ANSWER ONE OR TWO OR THREE?
Thanks
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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

Cheers
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My answer is 3
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Three objects created.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

https://coderanch.com/t/246349/java-programmer-SCJP/certification/Journal-Article-SCJP-Tip-Line
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Please confirm the correct answer is two or three, i'm kind of cofused.
Regards,
Simi
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
     
    Ranch Hand
    Posts: 69
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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 ]
     
    Just the other day, I was thinking ... about this tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic