How do handling String object in JVM?

  1. Java Virtual Machine maintains an internal list of references for interned Strings (Pool of unique Strings) to avoid duplicate String objects in heap memory.
  2. Whenever the JVM load String literal from the class file and executes, it checks whether that String already exists in the internal list or not.
  3. if Already exists in the list, String object does not create a new String and it uses reference to existing String  object.
  4. JVM does this type of checking for String literal but not for String object which it creates through 'new' keyword.
  5. You explicitly force JVM to do this type of checking for String objects which are creates through 'new' keyword using String.intern() method.
  6. This forces JVM to check the internal list and use the existing String object if it is already exist.

No comments:

Post a Comment