Java == for String对象停止工作? [英] Java == for String objects ceased to work?

查看:116
本文介绍了Java == for String对象停止工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public class Comparison {
        public static void main(String[] args) {
            String s = "prova";
            String s2 = "prova";
            System.out.println(s == s2);
            System.out.println(s.equals(s2));
        }
    }

输出:

true
true

在我的机器上。为什么?不应该==比较对象引用是否相等?

on my machine. Why? Shouldn't be == compare object references equality?

推荐答案

因为 字符串 实例是不可变的,Java语言能够进行一些优化,其中 String 文字(或更一般地, String ,其值是编译时常量) interned 并实际引用相同的(即 == )object。

Because String instances are immutable, the Java language is able to make some optimizations whereby String literals (or more generally, String whose values are compile time constants) are interned and actually refer to the same (i.e. ==) object.


JLS 3.10.5字符串文字



每个字符串文字都是对类String 的实例。 String 对象具有常量值。字符串文字 - 或者更一般地说,是作为常量表达式值的字符串 - 是实例化以便使用方法 String.intern

JLS 3.10.5 String Literals

Each string literal is a reference to an instance of class String. String objects have a constant value. String literals-or, more generally, strings that are the values of constant expressions -are "interned" so as to share unique instances, using the method String.intern.

这就是为什么你得到以下结果:

This is why you get the following:

System.out.println("yes" == "yes"); // true
System.out.println(99 + "bottles" == "99bottles"); // true
System.out.println("7" + "11" == "" + '7' + '1' + (char) (50-1)); // true
System.out.println("trueLove" == (true + "Love")); // true
System.out.println("MGD64" == "MGD" + Long.SIZE);

这就是说你应该 NOT 依赖 == for String 一般比较,应使用等于 for non - null instanceof String 。特别是,不要试图 intern()所有字符串,这样你就可以使用 == 不知道字符串实习是如何工作的。

That said it needs to be said that you should NOT rely on == for String comparison in general, and should use equals for non-null instanceof String. In particular, do not be tempted to intern() all your String just so you can use == without knowing how string interning works.

  • Java String.equals versus ==
  • difference between string object and string literal
  • what is the advantage of string object as compared to string literal
  • Is it good practice to use java.lang.String.intern()?

如果由于某些特殊原因你需要创建两个 String 对象(因此根据定义不是 == ),但是等于,然后你可以使用这个构造函数:

If for some peculiar reason you need to create two String objects (which are thus not == by definition), and yet be equals, then you can, among other things, use this constructor:


public String(String original) :初始化新创建的 String 对象,使其表示与参数相同的字符序列;换句话说,新创建的字符串是参数字符串的副本。 除非需要明确的原始副本,否则不必使用此构造函数,因为 Strings 是不可变的

public String(String original) : Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string. Unless an explicit copy of original is needed, use of this constructor is unnecessary since Strings are immutable.

因此,您可以:

System.out.println("x" == new String("x")); // false

new 运算符始终创建一个新对象,因此上面保证打印 false 。也就是说,这通常不是你真正需要做的事情。只要有可能,你应该只使用字符串文字,而不是为它显式创建一个 new String

The new operator always create a new object, thus the above is guaranteed to print false. That said, this is not generally something that you actually need to do. Whenever possible, you should just use string literals instead of explicitly creating a new String for it.

  • Java Strings: "String s = new String("silly");"
  • What is the purpose of the expression "new String(…)" in Java?

这篇关于Java == for String对象停止工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆