This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Strings

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I though I pretty much knew how strings worked. However while taking the exam questions at the back of the Strings chapter in the K&B book I got one of the "count how many string objects this code creates" questions wrong. I don't have the book in front of me, but the part that threw me off in the explanation of the answer was something like the following...



Why would this line create two string objects? I was under the impression that "ABC" should only exist once in the string pool once, not twice as the answer suggests.
[ September 22, 2005: Message edited by: Todd Johnson ]
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why would this line create two string objects? I was under the impression that "ABC" should only exist once in the string pool once, not twice as the answer suggests.

The literal "ABC" refers to a String object that is created when the class is loaded, with a reference stored in the constant pool. When you use the "new" keyword, you create a new String object, based on the contents (and possibly using the same char[] backing) as the object referred to by the literal.

The following article may be helpful in clearing up any confusion: String literals.

Cheers.
 
reply
    Bookmark Topic Watch Topic
  • New Topic