-4

具有以下内容:

String a = new String("test");
String b = "test";
System.out.println(a == b); //false

我们得到错误,因为String a它是一个对象,所以a指向与字符串文字不同的内存位置,b. 我想看看这是如何工作的,int并且Integer

Integer x = new Integer(5);
int y =5;
System.out.println(x == y); //true

我虽然那x.equals(y)会是真的,但x == y会是假的,因为在Strings. 我知道我们与 进行比较ints==但我认为将 anint与 an进行比较Integer会有所不同。为什么不是这样?

我假设在这种情况下 using==不能用于比较参考,那么我们将如何做(不确定这是否实用,但我想知道)?

4

1 回答 1

3

Because of boxing and unboxing in java

Converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing.

Autoboxing/unboxing is a pure convenience feature that allows you to assign values of a primitive type a reference of a wrapper class and vice versa, with the compiler automatically adding the code to convert between the two.

Boxing and unBoxing and SEE HERE ALSO

于 2013-06-01T12:09:54.217 回答