This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Strings

 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reference :- SCJP 6 k&b pg 434

It is written that,

String s= "abc" ;//Line 1 //creates one strig object .

In this simple case , "abc" will go in the pool and s will refer to it.

String s = new String("abc"); //Line 2 // creates two string objects

One object in non-pool memory
Another one in Pool memory

I have following two questions

Q1:- Why an object is also placed in a pool memory.

Q2:- As said by k&b that line 2 would create 2 objects in which one of them will go into pool, but after line 1 we have "abc" already in the pool, then why one more. Or the two statements are independent of one another.

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
These queries have been answered many times..you can read this FAQ

Also this could be useful http://www.coderanch.com/t/488922/java/java/new-keyword-objects-Java#2199295

Thanks
Yogi
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Yogesh !!!
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sahil Rally wrote:Reference :- SCJP 6 k&b pg 434

It is written that,

String s= "abc" ;//Line 1 //creates one strig object .

In this simple case , "abc" will go in the pool and s will refer to it.

String s = new String("abc"); //Line 2 // creates two string objects

One object in non-pool memory
Another one in Pool memory

I have following two questions

Q1:- Why an object is also placed in a pool memory.

Q2:- As said by k&b that line 2 would create 2 objects in which one of them will go into pool, but after line 1 we have "abc" already in the pool, then why one more. Or the two statements are independent of one another.


yes the two statements are independent of one another
you will understand by this example
when you do
then since these two strings have same character sequence so only one string object is shared by all string valued constant expressions.Such strings are said to be interned ,i.e they they share a unique string object if they have the same content,and the String class maintains a private pool where such strings are interned.
Now,let us take

This constructor creates a new String object and therefore it does not intern the string i.e it will not share the same value as shared by a and b.
and if you do

but i dont know whether another object goes into pool ,and if it goes then it would have printed true in above code ,but it doesnt.
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am concerned about "What you wrote in last" . Whether second time "abc" would go into pool as abandoned object or not !!!

 
Sheriff
Posts: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sahil Rally wrote:I am concerned about "What you wrote in last" . Whether second time "abc" would go into pool as abandoned object or not !!!


Sahil, I'm not really sure what your question is, but if I have this code

Now there will be only one "abc" in the String literal pool. The other String object referenced by s2 will not go into the String pool. If this is not what your question is, then can you please clarify your question a little...
 
Abhinav Yadav
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sahil Rally wrote:I am concerned about "What you wrote in last" . Whether second time "abc" would go into pool as abandoned object or not !!!


Ankit sir has clarified your doubt That

doesn't goes into the pool
its only the declarations of the type

are stored in pool
Hope you got it now!
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

abhinav yadav wrote:

Sahil Rally wrote:I am concerned about "What you wrote in last" . Whether second time "abc" would go into pool as abandoned object or not !!!


Ankit sir has clarified your doubt That

doesn't goes into the pool
its only the declarations of the type

are stored in pool
Hope you got it now!



Boss
when we say ,

String "WhatEvr will also go into String pool" inspite of an object that is referenced by s in non-pool memory !!!
I am concerned abt the object that goes into pool , like whatever in this case, if pool area already contains whatever, would it now 2 strings "whatever" in pool or just one !!!
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JLS states in section 3.10.5 String Literals (page 28) that:


Each string literal is a reference to a an instance of class String. String objects have a constant value. String literals, or more generally, strings that are the values of constant expressions are "interned" so as to share unique instances, using the method String.intern.



Thus, I can conclude that the String pool may contain the string literal "whatever". Nonetheless, the instance of the string assigned to the variable its not that on the string pool, but another instance.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!