- Java Virtual Machine maintains an internal list of references for interned Strings (Pool of unique Strings) to avoid duplicate String objects in heap memory.
- 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.
- if Already exists in the list, String object does not create a new String and it uses reference to existing String object.
- JVM does this type of checking for String literal but not for String object which it creates through 'new' keyword.
- You explicitly force JVM to do this type of checking for String objects which are creates through 'new' keyword using String.intern() method.
- This forces JVM to check the internal list and use the existing String object if it is already exist.
How do handling String object in JVM?
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment