首页 技术 正文
技术 2022年11月17日
0 收藏 708 点赞 2,120 浏览 1815 个字

首先声明,有晕车经历的司机请自备药物,String也可能让你怀疑人生!

老司机也晕车–java字符串String晕车之旅

第一道 开胃菜

请听题!第一道题:

        String hello="hello world!";        String hello1=new String("hello world!");
System.out.println(hello==hello1);
System.out.println(hello.equals(hello1));

老司机也晕车–java字符串String晕车之旅

提示: ==是比较两个对象引用是否正好引用到了相同的对象。

老司机也晕车–java字符串String晕车之旅

那么公布答案吧

false
true

旁白:

  1. new String是新建对象,和字符串常量对象不是同一个。
  2. equal是比较值

肯定不过瘾吧,那就再来。

第二道 汤

        String hello="hello world!";
String hello2="hello world!";
System.out.println(hello==hello2);
System.out.println(hello.equals(hello2));

老司机也晕车–java字符串String晕车之旅

扫地僧看不下去了

老司机也晕车–java字符串String晕车之旅

true
true

旁边:

两个String类型的常量表达式,如果标明的是相同的字符序列,那么它们就用相同的对象引用来表示。

第三道 副菜

        String hello="hello world!";
String append="hello"+" world!";
System.out.println(hello==append);
System.out.println(hello.equals(append));

老司机也晕车–java字符串String晕车之旅

那就公布答案

true
true

旁边:

两个String类型的常量表达式,如果标明的是相同的字符序列,那么它们就用相同的对象引用来表示。

第四道 主菜

        final String pig = "length: 10";
final String dog = "length: " + pig.length();
System.out.println(pig==dog);
System.out.println(pig.equals(dog));

老司机也晕车–java字符串String晕车之旅

不敢说了,还是公布答案吧

false
true

老司机也晕车–java字符串String晕车之旅

官方资料中有这么一段话:

Strings concatenated from constant expressions (§15.28) are computed at compile time and then treated as if they were literals.
Strings computed by concatenation at run time are newly created and therefore distinct.

老司机也晕车–java字符串String晕车之旅

翻译一下:

>通过常量表达式运算得到的字符串是在编译时计算得出的,并且之后会将其当作字符串常量对待.

>在运行时通过连接运算得到的字符串是新创建的,因此要区别对待。

看黑色重点标注。

第五道 蔬菜类菜肴

        final String pig = "length: 10";
final String dog = ("length: " + pig.length()).intern();
System.out.println(pig==dog);
System.out.println(pig.equals(dog));

先看答案吧

true
true

老司机也晕车–java字符串String晕车之旅

旁边:

可以通过显示的限定运算得到的字符串为字符串常量,String.intern方法可以”限定”

第六道 甜品

        final String pig = "length: 10";
final String dog = "length: " + pig.length();
System.out. println("Animals are equal: "+ pig == dog);
System.out.println("Animals are equal: "+ pig .equals(dog));

大家已经迫不及待了,先看答案

false
Animals are equal: true

老司机也晕车–java字符串String晕车之旅

如果你想一下操作符的优先级就明白了,“+”优先级高于“==”

老司机也晕车–java字符串String晕车之旅

第七道 咖啡、茶

老司机也晕车–java字符串String晕车之旅

看大家晕车严重,那就不出题目了

老司机也晕车–java字符串String晕车之旅

通过上面的教训,在比较对象引用时,应该优先使用equals 方法而不是 == 操作符,除非需要比较的是对象的标识而不是对象的值。

参考资料

【1】https://docs.oracle.com/javase/specs/jls/se12/html/jls-3.html#jls-3.10.5

【2】https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919