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.
Loading...
Related Quiz
- What is the purpose of a parameterized constructor in Java?
- The collect() method in the Stream API is a type of ________ operation.
- If we have a 2D array int[][] arr, the expression arr[i] refers to the __________.
- Which of the following is a primitive data type in Java?
- What will be the output of the following code snippet: public int add(int a, long b) { return a + b; } and public long add(int a, int b) { return a + b; }?