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()

最新文章

  1. DES加密中文乱码问题的解决
  2. asp.net 自带的缓存
  3. c调用python
  4. Struts2中上传图片案列
  5. PDT已有很大改进
  6. 【温故而知新-Javascript】使用 Document 对象
  7. 30Springd的包扫描——<context:component-scan base-package=” ”/>
  8. WPF:MenuItem样式
  9. 一步步学习NHibernate(1)——NHibernate介绍
  10. About Technology Research
  11. ceph基本操作整理
  12. 洞穴勘测(bzoj 2049)
  13. spring配置问题
  14. iOS学习——更改导航栏的返回按钮的标题与颜色
  15. PHP函数总结(个人版)
  16. php面向对象 封装继承多态 接口、重载、抽象类、最终类总结
  17. PHP数组排序函数array_multisort()函数详解(一)
  18. CentOS常用命令、快照、克隆大揭秘
  19. [Asp.net MVC]HandleErrorAttribute异常过滤器
  20. 使用hasOwnProperty监测对象是否含有某个属性

热门文章

  1. CSS——行业动态demo
  2. JS——Boolean(逻辑)对象
  3. 开发者自建IM服务器必须要解决的几个问题!
  4. [Windows Server 2012] Discuz X3安全设置
  5. GridView动态计算高度
  6. 4xx错误的本质:服务器已经接收到请求
  7. nuxt https
  8. dotfuscator 如何设置
  9. PAT_A1133#Splitting A Linked List
  10. redis下载安装配置教程