• 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

How many String Objects

 
Ranch Hand
Posts: 344
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,



How line 1 will creates two string. one referred to by x and the one is lost string "xyz"(this part confusioning)

Totally 4 string objects are created as by mock exam.

And also I seend one article about String java ranch, any body give that link.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we were passing a String to the Constructor which in turn will create a Sting, Am i right ?
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[code]
String str=new String["str"]
This will create 2 String object
One is the string which is pass to the constructor
That is ==>>"str"
and the other object is str which holds the value "str"
In this case the value pass to the constructor is lost
in the string pool
but str holds the reference of "str"

[code]
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the two literals "abc" and "xyz" both cause an object to be created in the string pool. that's 2

then, whenever you do a "new String(<something> ", another String object is created. that's 3 total.

As you may know, Strings are immutable. you cannot change one once it is created. so, you "x=x+y" does not just append the 'y' string to the 'x' string. it creates a brand new string object, making 4 total.
 
To do a great right, do a little wrong - shakepeare. twisted little ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic