Counting String objects created by Java code

前端 未结 13 1745
失恋的感觉
失恋的感觉 2020-12-05 14:04

How many String objects are created by the following code?

String x = new String(\"xyz\");
String y = \"abc\";
x = x + y;

I have visited ma

13条回答
  •  孤街浪徒
    2020-12-05 14:51

    1.objects created in heap area "xyz" //created by 'String x' and xyzabc //created by 'x+y'(concatenation)

    2.objects created in scp(string constant pool) "xyz" //created for future purpose which is not available for garbage collection and "abc" //created by 'String y' literal

    so total objects created in this case is 4

提交回复
热议问题