• 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

When are new String objects created

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

I am trying to resolve a conflict in information that is appearing in a java tutorial I am reading.

on the first page http://chortle.ccsu.edu/Java5/Notes/chap25/ch25_17.html it says that a new string object won't be created if the identical object already exists. Here this is described as an optimization.

on the second page http://chortle.ccsu.edu/Java5/Notes/chap26/ch26_20.html is an example that seems to directly contradict the first statement. In this example a second object is created when an identical object already exists. I verified this by running the example, and the == operator comparing the two references returned false.

So what am I missing?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whenever you say "new String", a new String object is created. Strings are shared from the pool when you jsut reference them like:
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "optimization" they were talking about in the first page was referring to assigning a String literal directly to the variable, as opposed to using the new keyword. The new keyword will always create a new object that has a different identity.

The page meant that the first time you use the literal "Green eggs", a new String object is created and remembered somewhere. The second time you use the literal "Green eggs", that object will be reused. This only works if you don't use the new keyword.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The difference is the presence of the keyword "new" which creates a new object.
If you are just referring to String literals, because strings are immutable (can't change) the compiler reuses them automatically.



a == b ? true
a.equals(b) ? true
a == c ? false
a.equals(c)? true

Because c is created with new String, it gets given a different String object than a and b.

 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the quick replies. It all makes sense now. And if I had read a few more pages I would have found the answer.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic