Get Started
1041

Q: What is the difference between creating String as new() and literal ?

  • Show 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.

The Most Comprehensive Exam Preparation Platform

Get the Examsbook Prep App Today