String Pool behavior

前端 未结 6 1628
迷失自我
迷失自我 2021-01-18 02:55

I read this Questions about Java's String pool and understand the basic concept of string pool but still don\'t understand the behavior.

First: it works if you

6条回答
  •  走了就别回头了
    2021-01-18 03:44

    This is my guess:

    String s1 = "a" + "bc"; String s2 = "ab" + "c";

    I think that are compile time these are determined to produce the same string and so only one object is made for both.

    But when you add "d" to both of them, this is done separately for both strings (since it's done during real time, there could be things like exceptions interrupting it etc, so it can't pre-do it) and so it doesn't automatically make them reference one object.

提交回复
热议问题