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

Java 什么时候应该在String文字上使用String的内部方法

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

Java 什么时候应该在String文字上使用String的内部方法

Java自动实习字符串文字。这意味着在许多情况下,==运算符似乎适用于

Strings
,其处理方式与
ints
或其他原始值的用法相同。

由于

Interning
对于
String
文字是自动的,因此该
intern()
方法将用于使用
new String()

使用你的示例:

String s1 = "Rakesh";String s2 = "Rakesh";String s3 = "Rakesh".intern();String s4 = new String("Rakesh");String s5 = new String("Rakesh").intern();if ( s1 == s2 ){    System.out.println("s1 and s2 are same");  // 1.}if ( s1 == s3 ){    System.out.println("s1 and s3 are same" );  // 2.}if ( s1 == s4 ){    System.out.println("s1 and s4 are same" );  // 3.}if ( s1 == s5 ){    System.out.println("s1 and s5 are same" );  // 4.}

将返回:

s1 and s2 are sames1 and s3 are sames1 and s5 are same


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

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

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