Join ExamsbookAnswer :
What is the difference between creating String as new() and literal ?5
Q: What is the difference between creating String as new() and literal ?
- Show AnswerHide Answer
- Workspace
Answer :
Explanation :
When we create string with new() Operator, it’s created in heap and not added into string pool while String created using literal are created in String pool itself which exists in PermGen area of heap. String s = new String("Test"); does not put the object in String pool , we need to call String.intern() method which is used to put them into String pool explicitly. its only when you create String object as String literal e.g. String s = "Test" Java automatically put that into String pool.