java int 和 Integer 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16836282/
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
Difference between int and Integer
提问by Java Questions
What is the difference between int
and Integer
. Yes, one is primitive
and another one is wrapper
, what is the situation to use them correctly.
是什么区别int
和Integer
。是的,一个是primitive
,另一个是wrapper
,正确使用它们的情况是什么。
Also what is the difference between :
还有什么区别:
int i=0;
++i
and
i++
回答by stinepike
part 1
第1部分
One example .. you can use Integer
as the key of HashMap
but you can't use int. Because an Object
is needed.
一个例子..你可以Integer
用作键,HashMap
但不能使用int。因为Object
需要一个。
So where you need an int
value as an object there you need to use Integer
class.
因此,在需要将int
值作为对象的地方,您需要使用Integer
类。
part 2
第2部分
++i is pre increment i++ is post increment
++i 是前增量 i++ 是后增量
for example
例如
i = 0;
System.out.println(i++) //will print 0 then the i will be 1.
and
和
i = 0;
System.out.println(++i) // here i wil be incremented first then print 1.
回答by Rahul Bobhate
Integer
is a wrapper class for int
which is a primitive data type. Integer
is used when int
can't suffice. For example: In generics, the type of the generic class, method or variable cannot accept a primitive data type. In that case Integer
comes to rescue.
Integer
是一个包装类,int
它是一个原始数据类型。不够Integer
用时使用int
。例如:在泛型中,泛型类、方法或变量的类型不能接受原始数据类型。在这种情况Integer
下来救援。
List<int> list; //Doesn't compiles
List<Integer> list; // Compiles
Moreover Integer
comes with a plethora of static methods, like toBinaryString
, toHexString
, numberOfLeadingZeros
, etc. which can come in very handy.
而且Integer
自带的静态方法,如登塔toBinaryString
,toHexString
,numberOfLeadingZeros
等它可以将非常方便。
回答by awsome
As already explained above An Integer is an object, whereas an int is a primitive. So you can have a null reference to an Integer and a Set or List of them. You can not do that with an int
正如上面已经解释过的 Integer 是一个对象,而 int 是一个原始类型。因此,您可以对 Integer 和它们的 Set 或 List 进行空引用。你不能用 int 做到这一点
I find this null reference very useful, when i have to store int values in database. I can store a null value when I use Integer. But cannot do so when I use int.
我发现这个空引用非常有用,当我必须在数据库中存储 int 值时。当我使用 Integer 时,我可以存储一个空值。但是当我使用 int 时不能这样做。
回答by SnakeDoc
A basic explanation is an int
is a primitive data type and literally is only a value stored in memory. An Integer
is a Java object that wraps an int
in a Class with lots of nice/helpful methods that can be called to work with that backing int
hidden inside. This is the same with most of the primitive data types, such as boolean
and Boolean
, char
and Character
, etc. This is refereed to as Boxing
a primitive. Unboxing being the opposite, taking an Object and extracting the backing primative.
一个基本的解释是 anint
是一种原始数据类型,字面上只是存储在内存中的值。AnInteger
是一个 Java 对象,它将 an 包装int
在一个类中,其中包含许多不错的/有用的方法,可以调用这些方法来处理int
隐藏在内部的支持。这与大多数原始数据类型相同,例如boolean
和Boolean
、char
和Character
等。这被称为原始数据类型Boxing
。拆箱是相反的,取一个对象并提取支持原语。
Here's an example of how one may use Integer
to convert a String
into an int
(boxed to an Integer
)
这是一个示例,说明如何Integer
将 aString
转换为int
(boxed to an Integer
)
String someString = "10";
Integer intObj = Integer.parseInt(someString);
System.out.println(intObj.toString());
You'll find that some of the data types have more helpful methods than others. Check the JavaDoc's for each of the types you are interested in, there are a lot of goodies in there!
您会发现某些数据类型比其他数据类型具有更多有用的方法。检查您感兴趣的每种类型的 JavaDoc,那里有很多好东西!
回答by Raedwald
An Integer
is an object, whereas an int
is a primitive. So you can have a null reference to an Integer
and a Set
or List
of them. You can not do that with an int
.
AnInteger
是一个对象,而 anint
是一个原始类型。因此,您可以对它们中的一个Integer
和一个Set
或一个进行空引用List
。你不能用int
.