4

Let's consider code below:

String s1 = "Hello world"
String s2 = "Hello world XXX"

I guess, that those strings are stored two strings in Java String Pool:

  • Hello world
  • Hello world XXX

How those strings are stored in memory of String Pool which is on heap? Are stored in special Heap for String Pool? Does any string's index exist for SP or those strings are stored in ordered collection? What happens if "Hello world AAA" is added into SP? Thanks.

jiri463
  • 859
  • 1
  • 8
  • 21
  • 1
    @AmitBhati no, there will be strings `"Hello world"` and `"Hello world XXX"` in the string pool. The fact the second starts with the first is irrelevant. – Andy Turner Jan 05 '17 at 13:30
  • See [this](http://stackoverflow.com/a/2486195/2815219) answer. – Raman Sahasi Jan 05 '17 at 13:31
  • Yes, but, in which structure are they stored? Another heap, sorted array? How those strings are compared? Because, when new string is added you have to look if it is not already exist in SP. – jiri463 Jan 05 '17 at 13:31

2 Answers2

0

Strings are immutable. For your example, there will be 2 objects in String Pool. If you are wondering about creating, here is very good post with simple explanation: http://www.journaldev.com/797/what-is-java-string-pool.

If you will use:

String s1 = "Hello World";
String s2 = "Hello World";
String s3 = "Hello WorldXXX";

Then there will be 2 objects, one for Hello World so then s1 == s2; is true (same object), but Hello WorldXXX will be another one object in memory.

Storing Strings in memory is defined by String interning, here is decsription on wiki.

For your comment: It depends on that if you call operator new, because then it is created in heap space (out of String Pool). If you use it like in my example, then they are stored in String Pool, where is decided that, if it already exists in String pool, then they are equals like objects (same memory in String Pool).

Also try to check https://en.wikipedia.org/wiki/Flyweight_pattern ;)

Hrabosch
  • 1,541
  • 8
  • 12
  • Yes, but, in which structure are they stored? Another heap, sorted array? How those strings are compared? Because, when new string is added you have to look if it is not already exist in SP. – jiri463 Jan 05 '17 at 13:36
  • Updated answer, I hope that I understand your question clearly. – Hrabosch Jan 05 '17 at 14:04
  • @jiri463 And they are compared with their content. But `Hello world` and `Hello world XXX` are two differents contents. This is if you DONT using operator _new_. – Hrabosch Jan 05 '17 at 14:07
0

One should distinguish what happens, when Java Virtual Machine allocates space for entities during compilation time (as in case of your example):

String s1 = "Hello world";
String s2 = "Hello world XXX";

and when space for strings is allocated during the runtime:

String s3 = new String("Hello world");
String s4  = new String("Hello world XXX");

In the first case, JVM structures all strings in memory in a heap. Every new occurence is compared with existing entries, if two values are the same, they point to the same address in memory space (in other words condition: s1 == s2 is true). This is possible, because String in Java is immutable, so its hashcode is cached at the time of creation and it doesn’t need to be calculated again.

In the second case, using new operator forces, JVM to allocate new space in memory model. This implies, that condition s1 == s3 is false. That is why it is recommended to use for string comparisons method: String.equals(), as it concerns about variable values, not memory addresses.

Tinki
  • 1,486
  • 1
  • 21
  • 32