• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

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.
 
A teeny tiny vulgar attempt to get you to buy our stuff
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!