• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String

 
ram prabu
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi to all

what is happening for below two lines

String s1="ram";

String s1=new String("ram")

Thks
Ram
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here
 
Ashika Chhabda
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Edmen Tay
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic