关于Java字符串String在运算中的拼接

来源:互联网 发布:php postgresql 编辑:程序博客网 时间:2024/04/25 00:23

看代码不解释。

        String s1 = "a";        String s2 = "b";        String s3 = s1 + s2;        String s4 = s1 + s2;        System.out.println(s3 == s4);        System.out.println(s3.equals(s4));        String s5 = "a";        System.out.println(s1 == s5);

看结果:

falsetruetrue

关于Java实现的步骤如下:
查看字符串常量池中是否存在相同的字符串对象。
若没有,则新创建一个包含该内容的字符串对象,并让引用变量指向该对象。例如,创建字符串s1的时候,字符串常量池中没有,则创建一个新对象,并让引用s1指向该对象。
若已经存在包含该内容的字符串对象,则让字符串引用直接指向该对象。例如,在创建字符串s5的时候,字符串常量池中已经有包含该内容的对象了,所以引用s2直接指向已有的对象。
如果是运算中拼接的,在常量池中是不存在的。如上结果可知。
增加代码:

System.out.println("--------------------------------");        String s6 = new String("a");        System.out.println(s1 == s6);        System.out.println(s1.equals(s2));

看结果:

--------------------------------falsetrue

自然new出来的,在堆中新开辟了一个地址,栈中的对象指向它
继续修改代码:

System.out.println("*********************************");        String s6 = new String("a");        System.out.println(s1 == s6);        System.out.println(s1.equals(s6));        System.out.println(s1.intern());        System.out.println(s1.intern()==s6.intern());        System.out.println(s1.intern()==s5.intern());

看结果

*********************************falsetrueatruetrue

关于Intern方法
查看jdk源码


Returns a canonical representation for the string object.
A pool of strings, initially empty, is maintained privately by the class String.

When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.

这里强调了只要s.equals(t) is true ,所有上面的代码的结果自然就清楚了。

All literal strings and string-valued constant expressions are interned. String literals are defined in section 3.10.5 of the The Java™ Language Specification.

Returns:
a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings.

附:jdk中文翻译:
intern
public String intern()返回字符串对象的规范化表示形式。
一个初始为空的字符串池,它由类 String 私有地维护。

当调用 intern 方法时,如果池已经包含一个等于此 String 对象的字符串(用 equals(Object) 方法确定),则返回池中的字符串。否则,将此 String 对象添加到池中,并返回此 String 对象的引用。

它遵循以下规则:对于任意两个字符串 s 和 t,当且仅当 s.equals(t) 为 true 时,s.intern() == t.intern() 才为 true。

所有字面值字符串和字符串赋值常量表达式都使用 intern 方法进行操作。字符串字面值在 Java Language Specification 的 §3.10.5 定义。
返回:
一个字符串,内容与此字符串相同,但一定取自具有唯一字符串的池。

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 扣扣上转账错了怎么办 qq绑卡存在异常怎么办 微信没绑银行卡忘记支付密码怎么办 微信的自动扣费怎么办 不小心把钱充到微信财付通该怎么办 财付通用什么充值卡充值话费怎么办 苹果手机灯坏了怎么办 手机电灯不亮了怎么办? 苹果手机相机坏了怎么办 苹果5s手机背光灯不亮怎么办 苹果5s灯控坏了怎么办 微信q币冲错号码怎么办 支付宝转账到之前号码怎么办 qq红包收不了钱怎么办 qq抢红包要实名认证怎么办 支付宝被骗冲q币怎么办 qq发红包发错了怎么办 qq红包发不出来怎么办 qq红包发多了怎么办 qq发红包要短信验证怎么办 不是qq好友发了红包怎么办 苹果手机升级后支付宝打不开怎么办 qqq币充了没进帐怎么办 怎么办q币换成qq余额 支付宝qb冲多了怎么办 微信没钱怎么办怎么赚 忘记微信支付密码怎么办 手机设备注册达上限怎么办 在新手机上登微信需要验证怎么办 手机号被限制注册qq号怎么办 手机互换了微信怎么办 微信申诉只有一位好友怎么办 微信申诉没有好友怎么办 四川电信多余的话费怎么办 四川电信话费多了怎么办 固话冲q币要密码怎么办 手机卡怎么突然没了怎么办 联通话费冲错了怎么办 王卡高额半停机 怎么办 计算机报考在手机上网上支付怎么办 建行app充话费不到账怎么办