Java 整数 a = 5 和 new Integer(5) 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18604768/
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
what is difference between integer a = 5 and new Integer(5)?
提问by user2745108
if i write below code(in java):
如果我写下面的代码(在java中):
Integer a =new Integer(5);
Integer b=new Integer(5);
if(a==b){
System.out.println("In ==");
}
if(a.equals(b)){
System.out.println("In equals");
}
My output is: "In equals" But if i change first and second line to ->
我的输出是:“In equals”但是如果我将第一行和第二行更改为 ->
Integer a =5;
Integer b=5;
then my o/p is:
那么我的 o/p 是:
In ==
In equals
So what is difference in creating a Integer object? How it gets created when we do Integer a =5
?
那么创建一个Integer对象有什么区别呢?当我们这样做时它是如何创建的Integer a =5
?
Does it mean that a and b object refer to same object, if i create Integer a=5
and creates another object Integer b=5
?
如果我创建Integer a=5
并创建另一个对象,这是否意味着 a 和 b 对象引用同一个对象Integer b=5
?
采纳答案by Evgeniy Dorofeev
Integer a = 5
; is called autoboxing, compiler converts this expression into actual
Integer a = 5
; 称为自动装箱,编译器将此表达式转换为实际
Integer a = Integer.valueOf(5);
For small numbers, by default -128 to 127, Integer.valueOf(int) does not create a new instance of Integer but returns a value from its cache. So here
对于小数,默认为 -128 到 127,Integer.valueOf(int) 不会创建 Integer 的新实例,而是从其缓存中返回一个值。所以在这里
Integer a = 5;
Integer b= 5;
a
and b
point to the same Object and a == b
is true
.
a
并b
指向同一个 Object 并且a == b
is true
。
回答by jbr3zy
for primitive data types like int
the equality operator will check if the variables are equal in value
对于基本数据类型,如int
相等运算符将检查变量值是否相等
for reference data types like your java.lang.Integer
objects, the equality operator will check if the variables reference the same object. In the first case, you have two "new" and separate integer objects, so the references are different
对于像您的java.lang.Integer
对象这样的引用数据类型,相等运算符将检查变量是否引用相同的对象。在第一种情况下,您有两个“新”和单独的整数对象,因此引用是不同的
回答by toths
In Java, you should never use the new Integer, even though it is valid syntax it is not the best way to declare integers as you have found out. Use instead Integer.valueOf(int) This has multiple advantages.
在 Java 中,您永远不应该使用新的 Integer,即使它是有效的语法,但正如您发现的那样,它不是声明整数的最佳方式。改用 Integer.valueOf(int) 这有很多优点。
You are not creating extra objects needlessly. Whenever you use the new operator you are forcing the vm to create a new object which is not needed in most cases. valueOf(int) will return a cached copy. Since Integer objects are immutable this works great. This means that you can use == though in practice you should use a null safe compare like in Apache ObjectUtils
您不会不必要地创建额外的对象。每当您使用 new 运算符时,您都在强制 vm 创建一个在大多数情况下不需要的新对象。valueOf(int) 将返回一个缓存副本。由于 Integer 对象是不可变的,因此效果很好。这意味着您可以使用 == 虽然在实践中您应该像在Apache ObjectUtils 中使用空安全比较
The == operator tests for equality. References are only equal when they refer to the same object in memory. equals methodensures 2 object instances are 'equivalent' to each other.
== 运算符测试相等性。引用仅在引用内存中的同一对象时才相等。 equals 方法确保 2 个对象实例彼此“等效”。
Integer a = new Integer(5);
Integer b = a;
a == b; // true!
a.equals(b); // true
b = new Integer(5);
a == b; // false
a.equals(b); // true
Primitives are equal whenever their value is the same.
只要它们的值相同,原语就相等。
int a = 5; int b = a;
a == b; // true!
回答by rocketboy
Integer wrapper shares few properties of String class. In that it is immutableand that can be leveraged by using intern() likefunctionality.
整数包装器共享 String 类的几个属性。因为它是不可变的,并且可以通过使用类似 intern() 的功能来利用。
Analyse this:
分析一下:
String a = "foo";
String b = "foo";
String c = new String("foo");
String d = new String("foo");
a == b //true
c == d //false
The reason is when JVM creates a new String object implicitlyit reusesexisting String object which has the same value "foo", as in case a and b.
原因是当 JVM隐式地创建一个新的 String 对象时,它会重用具有相同值“foo”的现有 String 对象,如 a 和 b 的情况。
In your case JVM implicitly auto-boxesthe ints and re-usesexisting Integer object. Integer.valueOf()
can be used explicitly to re-use existing objects if available.
在您的情况下,JVM 会隐式自动装箱整数并重新使用现有的 Integer 对象。Integer.valueOf()
如果可用,可以显式地用于重用现有对象。
回答by dganesh2002
I believe when you create using new operator it creates object reference. In first case, there are two object references and they are not same but their value is same. That is not the situation in second case.
我相信当您使用 new 运算符创建时,它会创建对象引用。在第一种情况下,有两个对象引用,它们不相同但它们的值相同。第二种情况并非如此。