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

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

Prepare for your Java exam with our ultimate quiz based on 'Thinking in Java'. Engage with expertly crafted questions that enhance your learning process. Perfect for Java enthusiasts looking to solidify their knowledge!

Each practice test/flash card set has 50 randomly selected questions from a bank of over 500. You'll get a new set of questions each time!

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.