How are strings stored in memory?
As we know that string can be created via 2 ways i.e by using sting literal (implicit) or by creating a string object using a new keyword.
STRING POOL
As we know that string can be created via 2 ways i.e by using sting literal (implicit) or by creating a string object using a new keyword.
STRING POOL
- Whenever a string is created via literal, the value is stored in special memory string pool.
- JVM looks if there is value is present on the string pool, the same value is allocated to the new string literal
- String pool will increase the memory efficiency and better performance
String s1 = "Hello"; // String literal
String s2 = "Hello"; // String literal
String s3 = s1; // same reference
STRING HEAP
- Whenever a string is created via new keyword, the value is stored in string heap.
- Each string object in a heap has its own storage
- There is no sharing of storage in heap even if two string have same content
String s4 = new String("Hello"); // String object
String s5 = new String("Hello"); // String object
0 comments:
Post a Comment