Mastering Java: The Ultimate Quiz for 'Thinking in Java'

Disable ads (and more) with a membership for a one time $2.99 payment

Mastering Java: The Ultimate Quiz for 'Thinking in Java'. Dive deep into Java with multiple-choice questions. Challenge yourself with quiz questions designed to test and improve your understanding of the 'Thinking in Java' book. Get ready for your exam!

Practice this question and more.


What purpose does the method intern() in String class serve?

  1. Converts the string to integer

  2. Internally sorts the string

  3. Ensures unique string references

  4. Optimizes memory usage for strings

The correct answer is: Ensures unique string references

The method intern() in String class serves the purpose of ensuring unique string references. Option A is incorrect because this method does not convert strings to integers. Option B is incorrect because it does not involve any sorting of strings. Option D is incorrect because it does not pertain to the purpose of the intern() method. The intern() method mainly helps to optimize memory usage by returning a string reference from the string pool if the same string already exists in the pool, instead of creating a new string object. This helps to avoid creating multiple string objects with the same value, thus saving memory. Therefore, the correct answer is C.