栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java中字符串的可重用性?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java中字符串的可重用性?

是的,为了简化起见,您可以将其视为从同一地址选择,但更精确的是变量拥有相同的 引用 ,即JVM 在映射到对象的正确内存地址时 使用的 数字/对象ID
(对象可以在内存中移动但仍然会有相同的参考)。

您可以使用以下代码进行测试:

String w1 = "word";String w2 = "word";String b = new String("word"); // explicitly created String (by `new` operator)          // won't be placed in string pool automaticallySystem.out.println(w1 == w2);  // true  -> variables hold same referenceSystem.out.println(w1 == b);   // false -> variable hold different references,         // so they represent different objectsb = b.intern(); // checks if pool contains this string, if not puts this string in pool,      // then returns reference of string from pool and stores it in `b` variableSystem.out.println(w1 == b);   // true  -> now b holds same reference as w1


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/498513.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号