More about Java Strings
* Immutable
* String Pool
Sujit Kumar
Zenolocity LLC ©2013-2024
Immutable Strings
• String s1 = “hello”;
• String s2 = “hi”;
• “hello” and “hi” are the objects in memory. These are
immutable => cannot change.
• But s1 and s2 are references to the objects.
• References are mutable. They can be changed to
point to different objects (of same type or inherited
type).
• s1 = “hola!”;
• Now s1 is pointing to a different object, that is allowed.
Immutable Strings
• “hello” object itself cannot change. “hi”
cannot change. “hola” cannot change.
• But references (pointers) to these objects can
change.
String Pool for String literals
• Special storage area in Java heap.
• When a string literal is used to create a string and if the
string already exists in the pool, the reference of the
existing string will be returned.
• Will NOT create a new object and return its reference.
• Saves valuable heap memory.
• String s1 = “hello”;
• String s2 = “hello”;
• Only 1 object with 5 chars is created by the above 2
statements.
• Both s1 and s2 point to that shared object.
• s1 == s2 will evaluate to true.
String Object
• Creating a String object with the new
operator WILL NOT place it in the STRING
POOL.
• String s3 = new String(“hello”);
• String s4 = new String(“hello”);
• s3 == s4 will be false
• Why String Pool ? -- Strings are very
commonly used. Sharing Strings from a pool
saves valuable heap memory.
CharSequence
• Interface
• Represents a readable sequence of char
values.
• Important classes implementing this interface
are String, StringBuffer and StringBuilder.
Why are Strings immutable?
• If string is not immutable, changing a given string
in the string pool with one reference will lead to
the wrong value for all the other references to
the same string in the string pool.
• Hashcode of String frequently used. Can be
cached & hence more efficient. No need to
recalculate it each time it is used.
• Removes security threats when accessing
network connections or files and also while
accessing classes via the Reflection API.

More about java strings - Immutability and String Pool

  • 1.
    More about JavaStrings * Immutable * String Pool Sujit Kumar Zenolocity LLC ©2013-2024
  • 2.
    Immutable Strings • Strings1 = “hello”; • String s2 = “hi”; • “hello” and “hi” are the objects in memory. These are immutable => cannot change. • But s1 and s2 are references to the objects. • References are mutable. They can be changed to point to different objects (of same type or inherited type). • s1 = “hola!”; • Now s1 is pointing to a different object, that is allowed.
  • 3.
    Immutable Strings • “hello”object itself cannot change. “hi” cannot change. “hola” cannot change. • But references (pointers) to these objects can change.
  • 4.
    String Pool forString literals • Special storage area in Java heap. • When a string literal is used to create a string and if the string already exists in the pool, the reference of the existing string will be returned. • Will NOT create a new object and return its reference. • Saves valuable heap memory. • String s1 = “hello”; • String s2 = “hello”; • Only 1 object with 5 chars is created by the above 2 statements. • Both s1 and s2 point to that shared object. • s1 == s2 will evaluate to true.
  • 5.
    String Object • Creatinga String object with the new operator WILL NOT place it in the STRING POOL. • String s3 = new String(“hello”); • String s4 = new String(“hello”); • s3 == s4 will be false • Why String Pool ? -- Strings are very commonly used. Sharing Strings from a pool saves valuable heap memory.
  • 6.
    CharSequence • Interface • Representsa readable sequence of char values. • Important classes implementing this interface are String, StringBuffer and StringBuilder.
  • 7.
    Why are Stringsimmutable? • If string is not immutable, changing a given string in the string pool with one reference will lead to the wrong value for all the other references to the same string in the string pool. • Hashcode of String frequently used. Can be cached & hence more efficient. No need to recalculate it each time it is used. • Removes security threats when accessing network connections or files and also while accessing classes via the Reflection API.