• 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
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
Bartenders:
  • Himai Minh
  • Carey Brown
  • Frits Walraven

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!!
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic