String intern() behaviour

前端 未结 4 891
借酒劲吻你
借酒劲吻你 2020-12-11 11:27

From the javaDocs of String class\'s intern method :

When the intern method is invoked, if the pool already contains a string equal to this String o

4条回答
  •  自闭症患者
    2020-12-11 11:49

    seven is the first time you use the string 'hello2'. Therefor what the intern does is insert your string to the pool (and also return it). there for it is equal to your seven.

    when you work with eight, the string is already in the pool (by running seven.intern() before, therefor when you do eight == eight.intern() you will get on the left side of the equation the newly created eight string, and on the right side the string created by seven from the pool, which are not the same

提交回复
热议问题