为什么“==”对整数和字符串引用的工作方式不同?
why "==" working differently for integer and strings reference?
我可以知道 ==
在这里是如何工作的吗?
public class App {
public static void main(String[] args) {
String s1 = new String("str");
String s2 = new String("str");
System.err.println("why it,s "+String.valueOf(s1==s2));
int i1 = new Integer(10);
int i2 = new Integer(10);
System.err.println("why it,s "+String.valueOf(i1==i2));
}
}
当我比较两个具有相同值的不同 String
的引用时,==
表示它们不相等,但是当我比较两个具有相同值的整数的引用时,然后 ==
说他们是平等的。为什么会这样?
好的,更有意义的比较是
public class App{
public static void main(String[] args) {
String s1 = new String("str");
String s2 = new String("str");
System.err.println("why it,s "+String.valueOf(s1==s2));
Integer i1 = new Integer(10);
Integer i2 = new Integer(10);
System.err.println("why it,s "+String.valueOf(i1==i2));
}
}
int
是原始的,== 只会比较值。 Integer
是一个对象,== for objects只有指向内存中同一个地方才为真,否则为假。对于基元(int、boolean 等...)== 将测试值。
int i = Integer(10)
实际上是 int i = (Integer(10)).intValue()
,java 只是自动执行。
编辑以添加更多解释:
Java 有 2 种类型。原始类型和 Class 类型。原始类型只是类型的值:int 原始类型就是 int。没有方法或任何东西,它只是一个整数。
如果说int a = 4
,a是原始变量,指向包含int的内存地址。就是这样。
Class 类型是 java 对象,因此 Integer 是 class 类型。它 表示 一个整数值,因此 Integer a = new Integer(4)
将创建一个整数对象,并且 a 将指向包含新的 对象的内存位置...创建.
有什么不同?
好问题。在后一个例子中(new Integer(4))这是一个整数和一些其他东西,像toString()
方法和一些其他东西
所以,回顾一下:
基元是存储在内存某处的值。
class 是一个存储在内存中某处的对象,在 Integer 的情况下,它代表一个 int 但它还有一些与之关联的其他东西(即 `toString() 方法)。
现在,== 是等效性测试。因此 int == int
检查原始值是否相同,因为这是原始类型具有的唯一信息。
== for objects does 意味着什么。由于对象指向的不仅仅是 4 或 8 或 -1,如果对象变量指向 内存中的相同位置 ,则 == 仅 returns 为真。如果没有,那么你得到 false。
所以:
int i = 1;
int j = 1;
Integer k = new Integer(1)
Integer l = new Integer(1)
Integer m = k;
Integer n = new Integer(k)
i == j -> true
k == l -> false
k == k -> true
k == l -> true
n == m -> false
n.intValue() == m.intValue() -> true (intValue returns a primitive type)
现在,.equals 是一个检查两个对象是否相等的测试,这对于不同的对象意味着不同的事情。
对于整数,如果 a 和 b 的 intValue() 相同,则 a.equals(b) 为真。但是,如果 a 和 b 指向 内存中的同一对象,则 a == b 为真
现在,关于其中包含 2 个整数(例如 x,y 坐标)的对象。
我可以知道 ==
在这里是如何工作的吗?
public class App {
public static void main(String[] args) {
String s1 = new String("str");
String s2 = new String("str");
System.err.println("why it,s "+String.valueOf(s1==s2));
int i1 = new Integer(10);
int i2 = new Integer(10);
System.err.println("why it,s "+String.valueOf(i1==i2));
}
}
当我比较两个具有相同值的不同 String
的引用时,==
表示它们不相等,但是当我比较两个具有相同值的整数的引用时,然后 ==
说他们是平等的。为什么会这样?
好的,更有意义的比较是
public class App{
public static void main(String[] args) {
String s1 = new String("str");
String s2 = new String("str");
System.err.println("why it,s "+String.valueOf(s1==s2));
Integer i1 = new Integer(10);
Integer i2 = new Integer(10);
System.err.println("why it,s "+String.valueOf(i1==i2));
}
}
int
是原始的,== 只会比较值。 Integer
是一个对象,== for objects只有指向内存中同一个地方才为真,否则为假。对于基元(int、boolean 等...)== 将测试值。
int i = Integer(10)
实际上是 int i = (Integer(10)).intValue()
,java 只是自动执行。
编辑以添加更多解释:
Java 有 2 种类型。原始类型和 Class 类型。原始类型只是类型的值:int 原始类型就是 int。没有方法或任何东西,它只是一个整数。
如果说int a = 4
,a是原始变量,指向包含int的内存地址。就是这样。
Class 类型是 java 对象,因此 Integer 是 class 类型。它 表示 一个整数值,因此 Integer a = new Integer(4)
将创建一个整数对象,并且 a 将指向包含新的 对象的内存位置...创建.
有什么不同?
好问题。在后一个例子中(new Integer(4))这是一个整数和一些其他东西,像toString()
方法和一些其他东西
所以,回顾一下:
基元是存储在内存某处的值。 class 是一个存储在内存中某处的对象,在 Integer 的情况下,它代表一个 int 但它还有一些与之关联的其他东西(即 `toString() 方法)。
现在,== 是等效性测试。因此 int == int
检查原始值是否相同,因为这是原始类型具有的唯一信息。
== for objects does 意味着什么。由于对象指向的不仅仅是 4 或 8 或 -1,如果对象变量指向 内存中的相同位置 ,则 == 仅 returns 为真。如果没有,那么你得到 false。
所以:
int i = 1;
int j = 1;
Integer k = new Integer(1)
Integer l = new Integer(1)
Integer m = k;
Integer n = new Integer(k)
i == j -> true
k == l -> false
k == k -> true
k == l -> true
n == m -> false
n.intValue() == m.intValue() -> true (intValue returns a primitive type)
现在,.equals 是一个检查两个对象是否相等的测试,这对于不同的对象意味着不同的事情。
对于整数,如果 a 和 b 的 intValue() 相同,则 a.equals(b) 为真。但是,如果 a 和 b 指向 内存中的同一对象,则 a == b 为真
现在,关于其中包含 2 个整数(例如 x,y 坐标)的对象。