aspose file tools*
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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: 36478
    
  16
Search the Ranch for "Strings literally" and you get a useful JavaRanch Journal article.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: String Object
 
Similar Threads
Object Create
== operator returns true for Strings
Strings and the string constant pool
Question to Kathy - from your book
Quick Q on String and GC