• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String literal vs String object

 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can u please tell me whats is the difference between a string literal & string object...

because i thought that any instnance of string is an object.

String name="XYZ";
String name1=new String("hjhj");


Thanks in advance.

ArKumar
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A String literal lives in the String object pool, if you create a String object using the new operator it lives on the heap (while you in your example also create a String literal in the String object pool).
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you my dear friend..

I found an interesting link too which offers further explanation


http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html


Thanks & have a nice time,
Aneesh.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic