• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Urgent String question

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reference K&B page 360 Ch6: java.lang According to Kathy:
String s=new String("abc"); //will result in two objects - one on the heap and
//the other("abc") in the string pool.
Well my Question is that if after the above statement we have:
String s1="abc";
Then will a new object be created on the pool or the reference of object already in the pool
will be assigned to S1.

Also if we originally had : String s="abc";
String s2=new String("abc");//2
then how many objects will be created on line 2?
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the compiler sees a String literal like "abc" the first time, it gets interned and placed on the String constants pool.
If you afterwards create a new String object like
String str=new String("abc");
only one object is created on the heap. However, should "abc" literal not have been encountered before, it would have been interned first, so you can say that 2 objects were created.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. After line 2, two objects will be created.
 
I love a woman who dresses in stainless steel ... and carries tiny ads:
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!