wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Memory allocation for String manipulation 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 "Memory allocation for String manipulation" Watch "Memory allocation for String manipulation" New topic
Author

Memory allocation for String manipulation

Prabhat Shankar
Greenhorn

Joined: Oct 08, 2009
Posts: 27
Hi,

Can Any one tell me what happen in back end for below code related to memory allocation:

String s1="prabhat";

S1=S1+"Shankar";


Prabhat Shankar Consumer Court,Consumer Court India,Consumer Complaints,Complaint India, Web Value
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

String s1="prabhat"; --------------> "prabhat"will be placed on pool

S1=S1+"Shankar"; ---------------> "Shankar" and "prabhatShankar" will be placed on pool.
Prabhat Shankar
Greenhorn

Joined: Oct 08, 2009
Posts: 27
Harpreet Singh janda wrote:String s1="prabhat"; --------------> "prabhat"will be placed on pool

S1=S1+"Shankar"; ---------------> "Shankar" and "prabhatShankar" will be placed on pool.



Harpreet

I am not getting properly....
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

Whenever you are creating a string without new operator, the string will be placed on shared pool. So the first line will place the literal on pool. In the second line you are concatenating the first string to second one and so second string being used in the statement will aging be placed on string pool and Because the result will be "prabhatShankar" which represents another string so this string will again be placed on Pool.

Hope this helps.
Prabhat Shankar
Greenhorn

Joined: Oct 08, 2009
Posts: 27
Harpreet Singh janda wrote:Whenever you are creating a string without new operator, the string will be placed on shared pool. So the first line will place the literal on pool. In the second line you are concatenating the first string to second one and so second string being used in the statement will aging be placed on string pool and Because the result will be "prabhatShankar" which represents another string so this string will again be placed on Pool.

Hope this helps.



Thanks Harpreet ........I got it....


but I have one doubt that what happens behind when we are using new keyword for String and for below code:

String S1="prabhat";
S1=S1+"prabhat";
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
String s1="prabhat";
String s2=s1+"Shankar";
String s3="prabhatShankar";
System.out.println(s3==s2);


s2 does not go in pool. New memory allocaition takes place each time + operation is done when one of the operand is variable.

If s2 were pooled, then System.out.println(s3==s2) would return true. In this case it returns false.


SCJP 1.4, SCWCD1.4, OCA(1Z0-007)
John Pradeep.v
Ranch Hand

Joined: Jul 21, 2008
Posts: 59
Naresh Chaurasia wrote:String s1="prabhat";
String s2=s1+"Shankar";
String s3="prabhatShankar";
System.out.println(s3==s2);


s2 does not go in pool. New memory allocaition takes place each time + operation is done when one of the operand is variable.

If s2 were pooled, then System.out.println(s3==s2) would return true. In this case it returns false.



thats right, to force JVM to pick the value from the pool you can use the below change which will instruct the JVM to use any constant of the same value if present in the pool.
well, this will work if your JVM has resolved your constant values while your class loads (there are different types of constant pool resolution mechanisms)

String s1="prabhat";
String s2=(s1+"Shankar").intern();
String s3="prabhatShankar";
System.out.println(s3==s2);


Regards,
John
Prabhat Shankar
Greenhorn

Joined: Oct 08, 2009
Posts: 27
Thank a lot All of you....

Harshana Dias
Ranch Hand

Joined: Jun 11, 2007
Posts: 327
Naresh Chaurasia wrote:String s1="prabhat";
String s2=s1+"Shankar";
String s3="prabhatShankar";
System.out.println(s3==s2);


s2 does not go in pool. New memory allocaition takes place each time + operation is done when one of the operand is variable.

If s2 were pooled, then System.out.println(s3==s2) would return true. In this case it returns false.



But it returns true know...i have tried
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
Refer to the following link Performance improvement techniques in String and StringBuffer for details on string and string buffer usage.
 
Don't get me started about those stupid light bulbs.
 
subject: Memory allocation for String manipulation