• 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
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Two ways to construct a String/pool of strings problem

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s1 = "Hello";
String s2 = "Hello";
String s3 = new String("Hello");
String s4 = new String ("Hello");
s1 and s2 point the same object (object A).
s3 and s4 point 2 different objects.
Does s3 point the same object as s1 and s2 (object A)?
--------------------------------------------------
If the order was different:
String s3 = new String("Hello");
String s4 = new String ("Hello");
String s1 = "Hello";
String s2 = "Hello";
Again s3 and s4 point 2 different objects (object A and object B respectively).
And s1 and s2 point same string object, but which object are s1 and s2 pointing at? A or B?
Many thanks!
Stasha
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stasha, use the equals operator == to test if two references refer to the same String object.

Does s3 point the same object as s1 and s2 (object A)? No
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

String.intern() returns a String that has the same contents as the one it is invoked on.
Any two strings with the same contents return the same String object from String.intern().
 
Stanislava Trajlov
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You!!
reply
    Bookmark Topic Watch Topic
  • New Topic