辨异 —— Java 中 String 的相等性比较
2024-09-02 20:26:31
How do I compare strings in Java?
1. 语法知识
==
:判断的是引用的相等性(reference equality),也即是否为同一对象;.equals()
:判断的是值的相等性(value equality),也即是否在逻辑上相等;
2. 举例
- new String(“test”).equals(“test”) // –> true
- // These two have the same value
- new String(“test”) == “test” // –> false
- // … but they are not the same object
- new String(“test”) == new String(“test”) // –> false
- “test” == “test” // –> true
- literals are interned by the compiler and thus refer to the same object
- Objects.equals(“test”, new String(“test”)) // –> true
- Objects.equals(null, “test”) // –> false
- // … but you should really just call Objects.equals()
最新文章
- DES加密中文乱码问题的解决
- asp.net 自带的缓存
- c调用python
- Struts2中上传图片案列
- PDT已有很大改进
- 【温故而知新-Javascript】使用 Document 对象
- 30Springd的包扫描——<;context:component-scan base-package=” ”/>;
- WPF:MenuItem样式
- 一步步学习NHibernate(1)&mdash;&mdash;NHibernate介绍
- About Technology Research
- ceph基本操作整理
- 洞穴勘测(bzoj 2049)
- spring配置问题
- iOS学习——更改导航栏的返回按钮的标题与颜色
- PHP函数总结(个人版)
- php面向对象 封装继承多态 接口、重载、抽象类、最终类总结
- PHP数组排序函数array_multisort()函数详解(一)
- CentOS常用命令、快照、克隆大揭秘
- [Asp.net MVC]HandleErrorAttribute异常过滤器
- 使用hasOwnProperty监测对象是否含有某个属性