What ever taught about intern pool i claim it is partially wrong!

Mam says if I declare String str = “hello”; and String str1 = “hello”; in order to first one str refers to the intern pool and checked whether the value “hello” present or not initially it wasn’t but once hello allocate in the intern pool after that when str1 has declared with the same value it refers to the same address, <---- this part I claimed wrong, because in order to refer same memory address with the same value we need to declare by —> String. intern() <— function

if,
String str = “hello”;
String str1 = str.intern(); was retuen then it should work ,

I am very sorry if I was wrong , basically this was my doubt please resolve :slight_smile:

hey @abhisekmaiti19 I am not able to get your doubt!!
but What I can grasp is that you are having a problem in the case when same value string is initialised(For eg : str = “hello” and str1 = “hello”) So when We initialised str the internpool checks if there is a string with value “hello”(Remember in intern pool only string is stored but the reference to that string (address) is stored in stack memory.)SInce there was no string the JVM creates a “hello” and pass this address to Stack and reference is stored there.But when we try to initialize str1 again JVM will check if there is string already present with a value “hello” since it is already there it will pass this address to stack and str1 will store address. So str and str1 will store same address in internpool. i.e. str = str1 = 4k and 4k = “hello” in internpool.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.