博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java String 的比较
阅读量:4311 次
发布时间:2019-06-06

本文共 905 字,大约阅读时间需要 3 分钟。

先看一段代码:

1 public class StringCom { 2  3     public static void main(String[] args) { 4         String a = "hello"; 5         String b = "hello"; 6         String c = new String("hello"); 7         char d[] = {'h','e','l','l','o'}; 8          9         System.out.println(a==b);  //true10 11         System.out.println(a==c);  //false12  13         System.out.println(a==d);  // Incompatible operand types String and char[]14 15         System.out.println(a.equals(d)); //false , 提示:Unlikely argument type for equals(): char[] seems to be unrelated to String16         17         System.out.println(a.equals(new String("hello")));  //true18 19     }20 21 }

遇到一个问题,就像上面的5个比较,问哪个会输出false,至于第13行的那个,在Eclipse里直接报错 “Incompatible operand types String and char[]  ”  , 所以意思是,也不会输出false?还没运行就报错了。

 

还有就是看到一个讨论: 

里面有关于 ‘==’和‘equals’的讨论,有些回复看得我懵逼了,推荐一篇博客:

还有知乎的一个回答:

 

转载于:https://www.cnblogs.com/Guhongying/p/11145111.html

你可能感兴趣的文章
SpringBoot2.0 + SpringCloud Eureka搭建高可用注册中心(Eureka之三)
查看>>
vue.js 组件-全局组件和局部组件
查看>>
svn 分支与合并的使用
查看>>
intellj idea
查看>>
浮动(clear)
查看>>
HierarchicalDataTemplate
查看>>
关于 keybd_event (vb篇)
查看>>
记公司的原型设计软件培训课程
查看>>
C语言知识点
查看>>
猴子吃桃
查看>>
tesseract-ocr图像识别技术(一)
查看>>
vs下dll与exe调试
查看>>
排列——递归
查看>>
cocos2d lua的cclog 在logcat中显示
查看>>
雅礼学习10.4
查看>>
Dota2 荒神罪 破解
查看>>
TP控制器的操作
查看>>
Installing Oracle Database 12c Release 2(12.2) RAC on RHEL7.3 in Silent Mode
查看>>
使用Metasploit进行端口扫描
查看>>
[LeetCode] 258. Add Digits
查看>>