gpt4 book ai didi

java - 当我们使用 String 的 intern 方法时创建了多少个对象

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:46:17 26 4
gpt4 key购买 nike

<分区>

我对 String 的 intern 方法不是很了解。

String  s1="java";  // should create  one  object in  String Constant  pool

String ss="java"; // no object is created (java is already in String pool)..it refers to object in String constant pool


String s2= new String("Android").intern(); // should create 2 objects one in heap and second in String constant pool

String s3= new String("java").intern()// i guess only one object is created on heap and s3 will point to object in String constant pool (as 'java' already exist).so the object in heap is lost because there is no reference

请让我知道我的理解是否正确?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com