wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String" Watch "String" New topic
Author

String

ram prabu
Greenhorn

Joined: Dec 23, 2008
Posts: 15
hi to all

what is happening for below two lines

String s1="ram";

String s1=new String("ram")

Thks
Ram
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

here


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Ashika Chhabda
Greenhorn

Joined: Dec 29, 2008
Posts: 3
Hi,
String str = "ram";
here is the concept of memory optimization i.e for String objects java provides memory optimization by creating a string literal into the String constant pool
example :

String s1 = "me";// "me" will be created in String constant pool and s1 is pointing to "me" now if
String s2 = "me";//"me" will be not be created again in String constant pool if alresdy there and now "s1 and s2" is pointing to "me"

this is just to save memory being occupied

Now

String s1 = new String("me");
here first of all "me" will be created in String constant pool and then because of new operator one more string literal "me" is created in memory

hence it create two string literals
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
String s1 = new String("me");
here first of all "me" will be created in String constant pool and then because of new operator one more string literal "me" is created in memory

hence it create two string literals


There is not String Object created on the String constant pool, pool contains only references to the String objects only. All String Objects are just like normal java objects, they are created at heap only, JVM maintains their references in the pool, so that Garbage collector thread cannot collect it.

Ya this is true that new operator will create a new String object "me", but it is not true that JVM will create two String literals. If "me" literal is not already on the heap, then this new "me" literal's reference will be saved in the string constant pool.


so here
String s1 = new String("me");

JVM will first create a String object "me" in the heap without looking for "me" literal, then it will try to look "me" reference in the constant pool, if no reference for "me" saved, then it will save reference of this new "me" in the pool.
[ December 29, 2008: Message edited by: punit singh ]

SCJP 6
Edmen Tay
Ranch Hand

Joined: Oct 21, 2008
Posts: 39

String s1="ram";//creates a new object of class String, and assigns it to the reference variable s1.

String s1=new String("ram")//creates a new object of class String, and assigns it to the reference variable s1.



if,
String s1 = new String();// "" assigns to the reference variable s1
just like
String s1 = "";

Strings is final class and it is Immutable Objects. you can read more on String, StringBuilder, and StringBuffer.

Hope this help.

Regards,
Edmen
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String