toString(),==,equals()对象方法在引用和基元类型上的工作方式有何不同或类似? [英] How does the toString(), ==, equals() object methods work differently or similarly on reference and primitive types?

查看:157
本文介绍了toString(),==,equals()对象方法在引用和基元类型上的工作方式有何不同或类似?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

toString()方法, == 运算符和等于( )方法在引用和基元类型上的工作方式不同或类似吗?

How does the toString() method, == operator, and equals() method work differently or similarly on reference and primitive types?

推荐答案

对于常规类型(包括String) ):

For regular types (including String):


  • == 比较对象引用。它测试两个对象引用是否相等;即如果它们引用相同的对象。

  • equals(Object)测试此对象是否等于另一个对象。 等于的含义取决于对象的类如何定义相等性。 java.lang.Object 类将等于(其他)定义为 this == other ,但很多类都会覆盖此定义。

  • toString()提供对象的简单转换一个字符串。结果String的格式和内容是特定于类的,并且(从 java.lang.Object 合同的角度来看)不能保证它有意义。

  • == compares object references. It tests if two object references are equal; i.e. if they refer to the same object.
  • equals(Object) tests if this object is "equal to" another one. What "equal to" means depends on how the object's class defines equality. The java.lang.Object class defines equals(other) to be this == other, but many classes override this definition.
  • toString() provides a simple conversion of the object to a String. The format and content of the resulting String is class specific, and (from the perspective of the java.lang.Object contract) there are no guarantees that it will be meaningful.

对于(真)原始类型:


  • == 比较类型的值,

  • equals()并且未定义 toString()。 Java不允许你在原始值上调用方法。

  • == compares values of the type, and
  • equals() and toString() are not defined. Java does not allow you to call a method on a primitive value.

然而,由于中的某些事实,这很复杂上下文 Java语言表示原始类型可以自动装箱以给出基本类型的相应包装类型的实例;例如 int 对应 java.lang.Integer ,依此类推。对于包装类:

However this is complicated by the fact that in some contexts the Java language says that a primitive type can be "autoboxed" to give an instance of the primitive type's corresponding wrapper type; e.g. int corresponds to java.lang.Integer, and so on. For the wrapper classes:


  • == 的定义与其他任何类似引用类型,

  • equals()比较包装值,

  • toString()格式化包装的值。

  • == is defined the same as for any other reference type,
  • equals() compares the wrapped values, and
  • toString() formats the wrapped values.

工程中的扳手如下所示:

The spanner in the works is illustrated by the following:

int a = ...
int b = a;
Integer aa = a;        // autoboxing occurs
Integer bb = b;        // autoboxing occurs

assert a == b;         // always succeeds
assert aa.equals(bb);  // always succeeds
assert aa == bb;       // sometimes succeeds, sometimes fails.

最后一次失败的原因是JLS不保证自动装箱给定的原始值总是给出相同的包装器对象。它在某些情况下(例如小整数),而不会用于其他情况(例如大整数)。

The reason that the last sometimes fails is that the JLS does NOT guarantee that autoboxing a given primitive value will always give the same wrapper object. It will in some cases (e.g. for small integers), and won't for others (e.g. large integers).

从上面的例子中学到的教训是你需要非常小心在引用类型上使用 == 。只有在确实想要测试两个引用是否属于同一个对象时才使用它。如果您只想测试对象是否相等而没有调用 equals()的开销,请不要使用它。

The lesson to be learned from the example above is that you need to be very careful about using == on a reference type. Only use it when you really want to test if two references are to the same object. Don't use it if you just want to test if the objects are "equal" without the overhead of calling equals().

(另请注意字符串是另一种类型,其中 == 会给你错误的答案在许多情况下;请参阅如何比较Java中的字符串?。 )

(Also note that String is another type where == is going to give you the wrong answer in many situations; see How do I compare strings in Java?.)

这篇关于toString(),==,equals()对象方法在引用和基元类型上的工作方式有何不同或类似?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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