Interview question
What is String interning and the intern() method? String interning क्या है और intern() method क्या करता है?
Answer
String s1 = "Hello";
String s2 = "Hello";
System.out.println(s1 == s2); // true
String s3 = new String("Hello");
String s4 = s3.intern();
System.out.println(s1 == s4); // trueString literals are automatically interned. The intern() method returns the canonical pooled representation.
String Interning:
1. Literals automatically interned
2. new String() creates a separate object
3. intern() returns the pooled representationWas this answer clear?