Java 在大多数情况下,您想要和需要的是equals()=测试值的相等性。int是一个“原语”,而不是一个引用。您只是在测试整数值。现在我知道我错在哪里了,但我也检查了您提出的解决方案(将int更改为integer),但结果仍然是真的,因此看起来好像发生了从int
Java 在大多数情况下,您想要和需要的是equals()=测试值的相等性。int是一个“原语”,而不是一个引用。您只是在测试整数值。现在我知道我错在哪里了,但我也检查了您提出的解决方案(将int更改为integer),但结果仍然是真的,因此看起来好像发生了从int,java,memory,reference,Java,Memory,Reference,在大多数情况下,您想要和需要的是equals()=测试值的相等性。int是一个“原语”,而不是一个引用。您只是在测试整数值。现在我知道我错在哪里了,但我也检查了您提出的解决方案(将int更改为integer),但结果仍然是真的,因此看起来好像发生了从integer到int的自动转换。根据oracle文档,情况不应该是这样的:“==运算符对整型表达式执行引用标识比较,对整型表达式执行值相等比较。“我已经测试过了,似乎java编译器在解包方面比以前更聪明了。@我们来看看我的答案,看看在解包之后发生了
在大多数情况下,您想要和需要的是
equals()
=
测试值的相等性。int
是一个“原语”,而不是一个引用。您只是在测试整数值。现在我知道我错在哪里了,但我也检查了您提出的解决方案(将int更改为integer),但结果仍然是真的,因此看起来好像发生了从integer到int的自动转换。根据oracle文档,情况不应该是这样的:“==运算符对整型表达式执行引用标识比较,对整型表达式执行值相等比较。“我已经测试过了,似乎java编译器在解包方面比以前更聪明了。@我们来看看我的答案,看看在解包之后发生了什么。”scene@Abb谢谢你的解释。我已经更新了我的示例以反映intValue缓存。“根据经验,最好将public、private或protected放在变量定义之前”-不同意,默认可见性有时是预期的,如果您确实想测试引用相等性,我相信有一个referenceEquals()
在Java中。@BrianJ我认为您无法访问本机类型的内存地址。@AbbéRésina发现我想到的是C。Java没有引用equals
,它使用=
,这意味着您必须是正确的。
public class Testing {
static int i = 47;
public static void main(String[] args) {
Testing t1 = new Testing();
Testing t2 = new Testing();
System.out.println(t1.i == t2.i);
public class Testing {
int i = 47;
public static void main(String[] args) {
Testing t1 = new Testing();
Testing t2 = new Testing();
System.out.println(t1.i == t2.i);
public class Testing {
Integer i = new Integer(47);
public static void main(String[] args) {
Testing t1 = new Testing();
Testing t2 = new Testing();
System.out.println(t1.i == t2.i); // false
}
}
if(t1.equals(t2)){
...
}
if(t1.getClass() == t2.getClass()){
...
}
if(t1 instanceof int){
...
}
public class Test {
public static void main(String...args) {
Integer h = new Integer(47); // Object created w/o boxing
Integer i = new Integer(47);
Integer j = 47; // Object created with boxing
Integer k = 47; // due to caching, this is the same Integer
Integer j2 = 247; // Object created with boxing
Integer k2 = 247; // no caching, these are different Integers
int l = 47; // primitives
int m = 47;
// compare two explicit Objects
System.out.println((h == i) ? "true" : "false"); // false
// compare one explicit Object with a autoboxed Object
// compare is reference compare
System.out.println((h == j) ? "true" : "false"); // false
System.out.println((j == h) ? "true" : "false"); // false
// compare two autoboxed Objects, compare is by reference
// because value was in cache range, the Integers are identical
System.out.println((k == j) ? "true" : "false"); // true
// compare two autoboxed Objects, compare is by reference
// because value was not in cache range, these are two Objects of type Integer
System.out.println((k2 == j2) ? "true" : "false"); // false
// adding a primitive to the compare will
// always compare by value
System.out.println((i == l) ? "true" : "false"); // true
System.out.println((m == l) ? "true" : "false"); // true
}
}
public class Foo {
final Integer i = 47;
final Integer j = 1234;
public static void main(String args[]) {
Foo p = new Foo();
Foo q = new Foo();
System.out.println(p.i.equals(q.i));
System.out.println(p.i == q.i);
System.out.println(p.j.equals(q.j));
System.out.println(p.j == q.j);
}
}
stack=2, locals=1, args_size=1
0: aload_0
1: invokespecial #11 // Method java/lang/Object."<init>":()V
4: aload_0
5: bipush 47
7: invokestatic #13 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
10: putfield #19 // Field i:Ljava/lang/Integer;
13: aload_0
14: sipush 1234
17: invokestatic #13 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
20: putfield #21 // Field j:Ljava/lang/Integer;
23: return