• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

number of reference objects in stringPool

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
I'd like to determine the number of strings created in the Heap(StringPool). What can I do when my i+1 variable is greater than the size of the array?
can you help me correct this code?



thanks
 
Rancher
Posts: 5035
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

help me correct this code


Can you explain why you think the code is not doing what you expect.  Can you post its output with some comments showing where you think there is a problem?

What can I do when my i+1 variable is greater than the size of the array?


Change the code so it does not use an index that is past the end of the array.
 
Tiam Bezalel
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there is an ArrayIndexOutOfBoundsException when 'i+1' is greater than the size of the ' varAgs' array.
how to make the loop end normally. for the moment i proceed as follows:

 
Norm Radder
Rancher
Posts: 5035
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you explain what the method is supposed to do?
Why does it use a loop to compare some of the elements in an array?  What is it looking for?
 
Tiam Bezalel
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
strings with the same hashCode
 
Sheriff
Posts: 4643
582
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tiam Bezalel wrote:strings with the same hashCode


The easiest way would probably be to use a Set:Note: using a HashSet has nothing to do with the values returned by Object#hashCode.

Also - this will provide you the number of unique hashcodes, not the number of string in the pool.
 
Tiam Bezalel
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there are actually two strings created in the string pool of the heap memory. although the two methods (yours and mine) give the same results, your approach is simpler. thank you for correcting me.
 
Norm Radder
Rancher
Posts: 5035
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

strings with the same hashCode


Can there be more than one hashCode with matches?  EG with hashCodes of 1,2,1,2,2  there are two matching values: 2 with value of 1 and 3 with value of 2.  Is that what you are looking for?  What value would the method return with those hashCodes
The posted code only compares two adjacent elements.  
Do you want to compare each element against all the other elements?

the two methods (yours and mine) give the same results,


I think you need better data for testing.  
Does Rod's method detect 2 different hashCodes?  Is that what you are looking for?
Your code detects that there are two instances when adjacent elements were the same.
 
Saloon Keeper
Posts: 10930
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to know all the Strings that have the same hash code...
 
Tiam Bezalel
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you want to compare each element against all the other elements?---> yes

I would like to compare the hash code of each string with the other strings in order to determine which strings have the same hash value. I will be able to determine the number of strings that are definitely created in the string pool.  

 
Norm Radder
Rancher
Posts: 5035
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

determine the number of strings that are definitely created in the string pool


Given the hashCodes of 1,2,1,2,2  what is the desired results?
2 unique hashCodes
max count of 3 with same hashCodes
 
Carey Brown
Saloon Keeper
Posts: 10930
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Output of histogram:
Note that the hash code is determined by the characters and sequence that they appear in the String. It has nothing to do with any "pool".
 
Tiam Bezalel
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

determine the number of strings that are definitely created in the string pool


Given the hashCodes of 1,2,1,2,2  what is the desired results?
2 unique hashCodes
max count of 3 with same hashCodes



yes !!!
 
Norm Radder
Rancher
Posts: 5035
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

yes !!!


Ok, which of the two choices is the one?
Or do you want both?
 
Tiam Bezalel
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

yes !!!


Ok, which of the two choices is the one?
Or do you want both?



Can you show me both?
 
Norm Radder
Rancher
Posts: 5035
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I have not written any code.  I was waiting for you to specify what the program was supposed to do.
Rod's code saves all the unique hashcodes.
To get a count for how often each hashcode is used, a Map might be useful.  The key would be the hashCode and the value would be the count of elements with that value
 
Tiam Bezalel
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Output of histogram:
Note that the hash code is determined by the characters and sequence that they appear in the String. It has nothing to do with any "pool".



I'm currently trying to better understand the creation of strings in the string pool of heap memory. Using the little program I've created, I'd like to determine the number of objects (strings of characters) that are created definitively in the string pool of heap memory.

Whenever a string literal is created, the compiler checks the String Constant Pool first. If it encounters the same string, then instead of creating a new string, it returns the same instance of the existing string to the variable.



two strings with the same hash value will be referenced in the string. here's the logic I'd like to implement

With my approach, Norm Radder's and yours, I think I've succeeded.

 
Sheriff
Posts: 28326
96
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember also that if two objects are equal, then they must have the same hash code. In particular, if two Strings are the same then they must have the same hash code. So you're writing code in an attempt to demonstrate the truth of this statement in one example.
 
Paul Clapham
Sheriff
Posts: 28326
96
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tiam Bezalel wrote:I'm currently trying to better understand the creation of strings in the string pool of heap memory. Using the little program I've created, I'd like to determine the number of objects (strings of characters) that are created definitively in the string pool of heap memory.



All you have demonstrated so far is that some strings which are identical have the same hash code. This is simply a rule of how hash codes behave and it has nothing to do with the system's string pools.
 
Tiam Bezalel
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

All you have demonstrated so far is that some strings which are identical have the same hash code. This is simply a rule of how hash codes behave and it has nothing to do with the system's string pools.



String pool is a storage space in the Java heap memory where string literals are stored. It is also known as String Constant Pool or String Intern Pool. It is privately maintained by the Java String class. By default, the String pool is empty. A pool of strings decreases the number of String objects created in the JVM, thereby reducing memory load and improving performance.



how can i determine the number of strings that are created in the string pool? i use the hashCode to implement this rule. should i proceed differently?

thanks
 
Paul Clapham
Sheriff
Posts: 28326
96
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two ways for you to create a string in the String Pool. Do you know what they are?
 
Tiam Bezalel
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are three popular ways of creating strings in Java:

   1-String literal
   2-Using new keyword
   3-Using String.intern() method


1-the first approach creates a string literal in the String Constant Pool.Whenever a string literal is created, the compiler checks the String Constant Pool first. If it encounters the same string, then instead of creating a new string, it returns the same instance of the existing string to the variable.

2-We can create new String objects using the new keyword. When we create new string literals using the new keyword, memory is allocated to those String objects in the Java heap memory outside the String Pool. However, we can stop this kind of memory allocation to String objects using the String.intern() method in Java.

3-Creating strings using the new keyword allocates memory to the string object in the heap but outside the string constant pool. When we use the String.intern() method, JVM puts the string literal in the String Pool (if not already present), and its reference is stored in the variable. However, if the String Constant Pool already contains a string equal to the String object to be created, its reference is returned.






 
Paul Clapham
Sheriff
Posts: 28326
96
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also create strings like this:


and so on. But yes, your description is generally correct.
 
Tiam Bezalel
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks .I need your advice on determining the total number of strings created in the stringPool. Is hashing the right approach?
 
Paul Clapham
Sheriff
Posts: 28326
96
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tiam Bezalel wrote:thanks .I need your advice on determining the total number of strings created in the stringPool. Is hashing the right approach?



Absolutely not. I thought we already discussed that.

I have no idea how to determine the contents of the string pool. And since the contents are initially created by the compiler, I don't see an easy way to do it. I'm not too fussed by that because I don't see the need for it. I'm not saying it's impossible, I'm just saying it's not especially meaningful.
 
Ron McLeod
Sheriff
Posts: 4643
582
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know how you can examine the heap programmatically (seems like it would be a security concern), but you could use a tool like VisualVM to inspect the heap on a running program or a saved heap dump.
 
Paul Clapham
Sheriff
Posts: 28326
96
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My totally impractical solution is to generate all possible Strings and interrogate each of them to find whether it's in the string pool. But the number of Strings you'd have to interrogate is outrageously astronomical.
 
Marshal
Posts: 79966
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tiam Bezalel wrote:hi,
I'd like to determine the number of strings created in the Heap(StringPool). . . .

Why? You have an automatic heap; trying to determine its contents sounds like keeping a dog and barking yourself.

. . .
  3-Using String.intern() method
. . .
When we use the String.intern() method, JVM puts the string literal in the String Pool (if not already present) . . .

Are you familiar with String#intern()? I don't think it creates a new object. Nor does it cause literals to be put into the String pool. You will find both those points explained in the link above.
 
Sheriff
Posts: 17696
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Tiam Bezalel wrote:hi,
I'd like to determine the number of strings created in the Heap(StringPool). . . .


Why? You have an automatic heap; trying to determine its contents sounds like keeping a dog and barking yourself.


That's a great question and an awesome analogy. I'd go further and liken it to getting a guard dog, tying it to the front door, and staying up all night to see if it barks.
 
Paul Clapham
Sheriff
Posts: 28326
96
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Are you familiar with String#intern()? I don't think it creates a new object.



The API wrote:When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.



The string pool, of course, must contain a collection of references to Strings, and so the intern must simply add a reference to the pool. No new String object would be created, as you say.

So it's possible to test whether a string is already in the string pool like this: if (s ==s.intern()) ... but that has the unfortunate side effect of adding the string to the pool.
 
Campbell Ritchie
Marshal
Posts: 79966
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . . intern must simply add a reference to the pool. . . .

Exactly!

Tiam Bezalel wrote:When we create new string literals using the new keyword, memory is allocated to those String objects in the Java heap memory outside the String Pool.

You can't create String literals at runtime; they are in the source code and are loaded into memory, in this case the String pool, when the class concerned is loaded.
 
Junilu Lacar
Sheriff
Posts: 17696
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a JUnit test that shows what is and what isn't a String in the Pool:

The assertSame() method checks if the two arguments refer to the same object in memory. That is, it checks for reference equality, just as the == operator would with two object references.
 
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic