-3

I am asking a duplicate Question, but i went through all the questions like this on Stack Overflow, but i am not able to Understand.

My Code is:

String s1 = "a";
String s2 = "b";
String s3 = s1 + s2;

1 . How many Objects are created? 2 . How can we say that objects are already present in String Table ?

-- Really Sorry for duplicity, but i must know the correct answer.

Ritesh Kaushik
  • 715
  • 2
  • 13
  • 24
  • Any link or answers will be appriciated. – Ritesh Kaushik Jan 30 '13 at 12:01
  • What do you mean by String Table? [String Pool](http://stackoverflow.com/questions/3801343/what-is-string-pool-in-java)? – Pshemo Jan 30 '13 at 12:06
  • http://stackoverflow.com/questions/3801343/what-is-string-pool-in-java and this http://stackoverflow.com/questions/1881922/questions-about-javas-string-pool – Bhavik Shah Jan 30 '13 at 12:07
  • If you want to downvote please do it, but give me an answer also?I know its a question asked thousand times, but i am not getting the explanation, i am not as intelligent as you guys. Do give answers, please. – Ritesh Kaushik Jan 30 '13 at 12:08

3 Answers3

1

Three Object will be created here in String pool.

String s1 = "a"; // 1st Object
String s2 = "b"; // 2nd Object
String s3 = s1 + s2; // "ab" 3rd Object.

if string is available in pool then it automatically pointing that object instead of creating new Object.

String str1 = "abc";
String str2 = "abc";

There will be one "abc" in string and both str1 and str2 pointing to same "abc". In case any modification happens like str2 +="d"; then it will create a new Object in pool "abcd" because once Object is created we can not change its value.

Note : String is immutable.

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
0

3 objects are created here.

The first two objects are obvious:

String s1 = "a"; // First
String s2 = "b"; // Second

The third object is a new object

String s3 = "a" + "b"; // Third

The only relation it has to s1 and s2 is that it contains the same letters as them, but it is a completely independent object. It's basically built like this:

String s3 = new String(s1.toString() + s2.toString());

Even if 2 strings contain the same character sequence they are not the same string.

String s4 = "foo";
String s5 = "foo";

EDIT

s4 == s5;       // Evaluates to true
s4.equals(s5);  // Evaluates to true

Both of the above will evaluate to true because of string pooling but are still separate objects. They use the flyweight design pattern in order to save memory.

On the other hand:

String s6 = new String("foo");
String s7 = new String("foo");

s6 == s7;      // Evaluates to false
s6.equals(s7); // Evaluates to true
enrybo
  • 1,787
  • 1
  • 12
  • 20
  • 2
    Actually `s4 == s5` will evaluate to `true` (thanks to String Pool it will be exactly same object). If you would like to make them different objects you would need to use one of String constructors like `String s4 = new String("foo")` – Pshemo Jan 30 '13 at 12:14
  • That's right thanks. I edited above. – enrybo Jan 30 '13 at 13:23
0

Three object will be created in the above example. As String is immutable, every time you try to change the value o string a new object will be created.

String s1 = "a" // 1st object
String s2 = "b" // 2nd object
String s3 = s1+ s2 // in this case a new onject with value "ab" will be created