• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Clarification on String Objects for SCJP

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String sss = new String("abc");
This will create an obj on the heap that is referenced by sss. Will it also have a reference in the String Literal pool?
If I had said String sss = "abc" only then it would have created a reference to them from the String Literal Pool.Am I right?

This is another question.
If I were to say
String s1 = new String("abc");
s2="abc";

Will this create a new object for s2 ?, Since there is already a reference to the string object("abc") in the string literal pool. s2 will now refer to the obj created in line 1 and refered to by s1

[ January 26, 2006: Message edited by: Abid Sami ]

[ January 26, 2006: Message edited by: Abid Sami ]

[ January 26, 2006: Message edited by: Abid Sami ]
[ January 26, 2006: Message edited by: Abid Sami ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may want to read Corey McGlone's JavaRanch article, Strings, Literally. This should clear up your doubts.
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, s2 will not reffer to the same object s1 reffers to, it will reffer to the "abc" in the string pool not the one on the heap. You can check it very easily, find out what`s the result of s1 == s2.
 
Poop goes in a willow feeder. Wipe with this tiny ad:
ScroogeXHTML 9.0 - RTF to HTML5 and XHTML converter
https://coderanch.com/t/731949/ScroogeXHTML-RTF-HTML-XHTML-converter
    Bookmark Topic Watch Topic
  • New Topic