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

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

Question: 1 / 515

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

Converts the string to integer

Internally sorts the string

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.

Optimizes memory usage for strings

Next

Report this question