0

I am learning string implementations in java, got the following doubt while learning

String s1 = "name1";
String s2 = "name1"; // both strings having same value;

I know that,

s1==s2 

above comparison will return false (based on what i studied, == compares the memory location of the string objects).

but this returns true;

  1. I want to know the reason why its returning 'true'?
  2. Also how the memory will be allocated for a string literal in string pool(what really happens inside the string pool/back end);

for the following operations,

String s1 = "String1";
String s2 = s1+"xyz";
String s3 = s1.replace("i","o");
lourdh
  • 449
  • 2
  • 12
  • 30
  • @SotiriosDelimanolis: I have mistakenly put s2="name2" instead of "name1"; could you tell me what will be the result for other code snippets. – lourdh Jun 11 '14 at 01:49
  • Why don't you just try to run it and see? – Sotirios Delimanolis Jun 11 '14 at 01:51
  • @SotiriosDelimanolis: I tried it, but what i want to know is how the string literals will be added into string pool. 1. whether s2 creates a new string with "String1xyz" or will it create "xyz" and point to s1&"xyz"; 2. similarly will s3 create new string "Strong1" in string pool? I got the output but want to know the logic? or is there any way to check the string literals inside string pool? – lourdh Jun 11 '14 at 02:01
  • Do a search of this site or on Google. So much has been written about this. – Sotirios Delimanolis Jun 11 '14 at 02:58

0 Answers0