• 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

String objects  RSS feed

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is said that if we declare a atring object like
String ab="abc";
then the jvm will first check the String constant pool for the existence of some object with value "abc",if it exists it will assign it to ab else a new object is created.
but when we use new keyword to make a new object like
String ab=new String("abc");
then memory from non-String pool is assigned to the object and a new object is created.
QUESTION:
what if we use the new keyword but the constant pool already contains a literal entry of "abc".
then will the jvm create a new object without checking the pool or not.
and to which object will the ref variable ab refer to??
 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I know if you use the new keyword a new object will be created outside of the string pool and no checking of the string pool is peformed.

It's better if you can a write small program to verify this anyway.
 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do i verify that to which object is it refering to??
and is it from the string pool or not
 
Bartender
Posts: 4050
54
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use "==" to check whether two String objects refer to the same memory location or not. That's the best way you can check this according to my knowledge.



will result



Try it with different ways you want.
 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ohk, thanks Vijitha Kumara
 
Java Cowboy
Sheriff
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you create a String object with the "new" keyword, you will always get a new String object that is not in the pool, no matter if there's already a string in the pool with the same contents or not.

You can call intern() on a String object. That method returns a reference to a String object in the following way: If there's a string in the pool with the same content, then it returns the existing string in the pool; if it's not in the pool, then a new string is created in the pool and a reference to that new pooled string is returned. See the API documentation for more information.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!