This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes String Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String Object " Watch "String Object " New topic
Author

String Object

Rishabh Shah
Ranch Hand

Joined: Nov 30, 2010
Posts: 36
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

Joined: Jul 13, 2011
Posts: 6

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

Joined: Nov 30, 2010
Posts: 36
Thanks Ganesh...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
Search the Ranch for "Strings literally" and you get a useful JavaRanch Journal article.
 
GeeCON Prague 2014
 
subject: String Object