1

How many objects will be create for the following string on the heap:

String a = "abc";
String b = "def";
String c = "ghi";
String d = a+b+c

How many objects are created on the heap?

Maroun
  • 94,125
  • 30
  • 188
  • 241
yogesh2510
  • 81
  • 1
  • 1
  • 3

1 Answers1

1
String a = "abc";  //String literal
String b = "def";  //String literal
String c = "ghi";  //String literal
String d = a+b+c   //String d = new StringBuilder(d).append(b).append(c).toString();

Note that StringBuilder#append returns an object. Now it's easy for you to determine how many objects were created..

Also note that toString doesn't create a new String, it returns an already created one.

Maroun
  • 94,125
  • 30
  • 188
  • 241