• Post Reply Bookmark Topic Watch Topic
  • New Topic

Referance of this two string should be same but execution says its not  RSS feed

 
Mamata Gelanee
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




Output is : s1 and s2 has same content

if after concatenation both string is stored in string pool it will refer to same memory location
and other confusion is after any string operation like (concatenation , replace , substring) jvm creates a new string in which memory area..?? Heap or String pool..??



Thank you
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your example proved that s1 is not in string pool. That's why you should never use == to compare strings.
 
Mamata Gelanee
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Your example proved that s1 is not in string pool. That's why you should never use == to compare strings.



I know what you trying to say.. But i just want to know where this string will stored..??
if in heap memory then what about effective memory utilization using string pool..??

Thank you
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mamata Gelanee wrote:I know what you trying to say.. But i just want to know where this string will stored..??

Why do you need to know this?
 
Mamata Gelanee
Greenhorn
Posts: 11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:
Mamata Gelanee wrote:I know what you trying to say.. But i just want to know where this string will stored..??

Why do you need to know this?


There is a thing called "Curiosity" exist in world..
:-p
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mamata Gelanee wrote:
Paweł Baczyński wrote:Your example proved that s1 is not in string pool. That's why you should never use == to compare strings.



I know what you trying to say.. But i just want to know where this string will stored..??
if in heap memory then what about effective memory utilization using string pool..??

Thank you


The string pool is for string literals or anything interned during runtime.
It isn't added to during runtime as a matter of course.

So in your example the concat() call does not automatically intern the resulting string.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I understand it, String LITERALS are stored in the pool - or things the compiler can determine as such. There may even be some optimizing if you have something like:

String temp = "fred" + " says hi"

which would store all three in the pool.

However, because the concat doesn't actually happen until runtime, the compiler can't figure out the result, and therefore, can't put it in the pool.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mamata Gelanee wrote:I know what you trying to say.. But i just want to know where this string will stored..??


It's an object, so it's stored where all the other objects are stored.

if in heap memory then what about effective memory utilization using string pool..??


Yes, in the heap memory then. But what's your question about "effective memory utilization"? Were you thinking that it would be more effective to put all String objects into the string pool? What made you think that?
 
Mamata Gelanee
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Mamata Gelanee wrote:I know what you trying to say.. But i just want to know where this string will stored..??


It's an object, so it's stored where all the other objects are stored.

if in heap memory then what about effective memory utilization using string pool..??


Yes, in the heap memory then. But what's your question about "effective memory utilization"? Were you thinking that it would be more effective to put all String objects into the string pool? What made you think that?



All the strings stored in string pool is reused if encounter again... so it is effective memory utilization..please read about string pool and why it is used in java..

Thank you
 
Mamata Gelanee
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:As I understand it, String LITERALS are stored in the pool - or things the compiler can determine as such. There may even be some optimizing if you have something like:

String temp = "fred" + " says hi"

which would store all three in the pool.

However, because the concat doesn't actually happen until runtime, the compiler can't figure out the result, and therefore, can't put it in the pool.




May be you are right..

What all people think..??
Or if I can get official document about it , if it exists..

Thank you all for your help

 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mamata Gelanee wrote:All the strings stored in string pool is reused if encounter again... so it is effective memory utilization..please read about string pool and why it is used in java.


So what about the case where the code creates thousands of new String objects which are never encountered again? Would it be effective to store them all in the string pool? I don't believe it would be, but maybe you can demonstrate why it would be.

As for the string pool, its main purpose is to provide a place for those String objects which don't have references (namely String literals) but which shouldn't be garbage-collected. There isn't a pool for other types of object built into the JVM because other types of objects can't be represented by literal. String needs special handling to support literals and the string pool is that special handling.

But if you can point to some document which suggests it would be helpful to put all String objects into that pool, I'm willing to read it.
 
Mamata Gelanee
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:

As for the string pool, its main purpose is to provide a place for those String objects which don't have references (namely String literals)


From SCJP 6 book by Kethy siera :

One of the key goals of any good programming language is to make efficient use of memory. As applications grow, it's very common for String literals to occupy large amounts of a program's memory, and there is often a lot of redundancy within the universe of String literals for a program. To make Java more memory efficient, the JVM sets aside a special area of memory called the "String constant pool." When the compiler encounters a String literal, it checks the pool to see if an identical String already exists. If a match is found, the reference to the new literal is directed to the existing String, and no new String literal object is created.

I think this is main purpose of string pool in java..
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mamata Gelanee wrote:I think this is main purpose of string pool in java..


That's absolutely correct. However you'll notice that the entire focus of that paragraph is String literals. It says nothing at all about other String objects which might be produced as the program runs, which was the misunderstanding which led to your original question. So it looks like you have the concept clearer in your mind now.

It's also true that if there are duplicate String literals in the code, then only one copy of the duplicate literals will be stored in the string pool. This naturally leads to efficiency in memory usage. However the string pool is needed even if there are no duplicate String literals anywhere, for the reason I mentioned before. So I wouldn't say that memory efficiency is its primary purpose, it's just a useful side effect of the way the pool is structured.
 
Mamata Gelanee
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok..
Now just help me to clear this ..
As in my code both strings have same content but they are stored at different memory locations..
So it is not necessary if both string has same content they share same location..
Am I right..??
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that sounds right. But you don't need to know it.
 
chi thanh
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mamata Gelanee wrote:



Output is : s1 and s2 has same content

if after concatenation both string is stored in string pool it will refer to same memory location
and other confusion is after any string operation like (concatenation , replace , substring) jvm creates a new string in which memory area..?? Heap or String pool..??



Thank you

I think like you, thanks....



--------------------
parallaxify
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!