Counting String objects created by Java code

前端 未结 13 1732
失恋的感觉
失恋的感觉 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:43

    The answer is 5

    1. xyz in non pool memory
    2. xyz in pool memory with no reference
    3. abc in pool memory with reference
    4. xyz still in non pool memory, reference changed to xyzabc in non pool memory
    5. xyzabc in pool memory with no reference

提交回复
热议问题