java 相等运算符如何处理原始和对象类型数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29139274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:44:26  来源:igfitidea点击:

how equal operator works with primitive and object type data

javaequalityunboxing

提问by Still Learning

I know its a very basic question but I want to be clear about the concept. I want to know how ==operator works in case of primitive and object type. For example

我知道这是一个非常基本的问题,但我想弄清楚这个概念。我想知道==运算符在原始类型和对象类型的情况下如何工作。例如

Integer a = 1;
int b = 1;
System.out.println(a == b)

how ais compared with b, whereas acontain the ref of object that contains value 1. Can somebody clearify it to me how it works internally?

如何ab,进行比较,而a包含包含值 1 的对象的引用。有人可以向我澄清它在内部是如何工作的吗?

回答by akhilless

In general the equality operator in Java performs a so called shallow comparison. In other words it compares the values that variables contains. Now the variables of primitive data types contains the value itself while the reference types contains reference to heap area which stores the actual content. That means that in your code snippet int bwill hold value 1and Integer awill hold the memory address of the actual Integer object on the heap.

通常,Java 中的相等运算符执行所谓的浅层比较。换句话说,它比较变量包含的值。现在原始数据类型的变量包含值本身,而引用类型包含对存储实际内容的堆区域的引用。这意味着,在您的代码段int b将持有价值1Integer a将举行堆上的实际Integer对象的内存地址。

Now in the particular example provided by you there is one pecularity. Integer class a special wrapper class that wraps primitive integer types. The compiler can automatically convert between such wrapper objects and primitive types (which is called boxing and unboxing).

现在,在您提供的特定示例中,有一个特点。整数类是一种特殊的包装类,用于包装原始整数类型。编译器可以自动在此类包装器对象和原始类型之间进行转换(称为装箱和拆箱)。

Let's walk you code step by step tgo make it clear.

让我们一步一步地教你写代码 tgo 说清楚。

Integer a = 1;

The compiler actually substitue the following code insted of this line:

编译器实际上替换了这一行的以下代码:

Integer a = Integer.valueOf(1);

The static method valueOfreturns an wrapper object instance that wraps the provided primitive value. This procedure when the compiler constructs a wrapper class out of a primitive type is called boxing.

静态方法valueOf返回包装提供的原始值的包装器对象实例。编译器从原始类型构造包装类时的这个过程称为装箱。

When using such a wrapper object is compared with with a primitive variable using equality operator

当使用这样的包装对象与使用相等运算符的原始变量进行比较时

a == b

the compiler actually changes it to the following:

编译器实际上将其更改为以下内容:

a.intValue() == b;

where intValuereturns the primitive value wrapped by the wrapper object (which is called unboxing) i.e. it unboxes the primitive value and make the expression equivalent to comparing two primitives. This is why the equality operator then returned true

whereintValue返回由包装器对象包装的原始值(称为拆箱),即它拆箱原始值并使表达式等效于比较两个原始值。这就是为什么相等运算符然后返回的原因true

回答by Sergey Pauk

In your particular example boxed type Integer will be unboxed to a primitive type int and ==will compare primitives (i.e. true in your case).

在您的特定示例中,装箱类型 Integer 将被拆箱为原始类型 int==并将比较原始类型(即在您的情况下为 true )。