Which of the following statements are true regarding the intern() method of the String class?

  • Calling intern() on a String can reduce memory usage by ensuring only one copy exists in the string pool.
  • The intern() method adds the String to the string pool.
  • The intern() method is only available in Java 9 and later.
  • The intern() method returns a new String object.
The intern() method of the String class is used to add the String to the string pool if it's not already there and returns a reference to that String. This can reduce memory usage by ensuring only one copy of a particular string exists in the string pool, which is useful for memory optimization. The intern() method has been available since early versions of Java, not just in Java 9 and later.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *