Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Object

 
Rishabh Shah
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Good Morning Everyone...

I want to ask question regarding String....

We can create String object with two different ways

String string = "Rishabh";
Here, It creates one reference variable string and one String Object in Pool Memory.

String string = new String("Rishabh");
Here, It creates one reference variable string and one String Object in Normal (Non-Pool) memory and other object in Pool Memory where "Rishabh" will be placed.


I understood first way..Now, I want to understand second way in terms of memory..how does it create 2 different object in memory.

Thanks & Regards,
Rishabh Shah

 
Ganesh Akondi
Greenhorn
Posts: 6
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first one String string = "Risabh" is compile time constant.
second one String string = new String("Risabh") will be created at run time and is not a compile-time constant. The JVM loads the String pool when the class is loaded.

Hope this thread helps you more:
http://www.coderanch.com/t/552653/java/java/string-immutable
 
Rishabh Shah
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ganesh...
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Search the Ranch for "Strings literally" and you get a useful JavaRanch Journal article.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic