Tuesday, April 15, 2014

 What is the difference between creating String with new operator and double qoutes?


String literal stored in String Pool Area (SPA)
String Instance stored in both  SPA and Heap Memory (HM)

When you create string with quotes it is stored in SPA
When you create string with new operator it is not always stored in HM,
  first it checks if string already created, if so reference just point to that, similarly with SPA

Hence String always created in SPA and HM based on existence,

I mean if you are trying to create duplicate string, Java won't create new string in memory.