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?
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?
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.