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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:07:46  来源:igfitidea点击:

Difference between int and Integer

javaintegerintwrapperprimitive-types

提问by Java Questions

What is the difference between intand Integer. Yes, one is primitiveand another one is wrapper, what is the situation to use them correctly.

是什么区别intInteger。是的,一个是primitive,另一个是wrapper,正确使用它们的情况是什么。

Also what is the difference between :

还有什么区别:

int i=0;
++i
and 
i++

回答by stinepike

part 1

第1部分

One example .. you can use Integeras the key of HashMapbut you can't use int. Because an Objectis needed.

一个例子..你可以Integer用作键,HashMap但不能使用int。因为Object需要一个。

So where you need an intvalue as an object there you need to use Integerclass.

因此,在需要将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

Integeris a wrapper class for intwhich is a primitive data type. Integeris used when intcan't suffice. For example: In generics, the type of the generic class, method or variable cannot accept a primitive data type. In that case Integercomes to rescue.

Integer是一个包装类,int它是一个原始数据类型。不够Integer用时使用int。例如:在泛型中,泛型类、方法或变量的类型不能接受原始数据类型。在这种情况Integer下来救援。

List<int> list;   //Doesn't compiles
List<Integer> list;  // Compiles

Moreover Integercomes with a plethora of static methods, like toBinaryString, toHexString, numberOfLeadingZeros, etc. which can come in very handy.

而且Integer自带的静态方法,如登塔toBinaryStringtoHexStringnumberOfLeadingZeros等它可以将非常方便。

回答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 intis a primitive data type and literally is only a value stored in memory. An Integeris a Java object that wraps an intin a Class with lots of nice/helpful methods that can be called to work with that backing inthidden inside. This is the same with most of the primitive data types, such as booleanand Boolean, charand Character, etc. This is refereed to as Boxinga primitive. Unboxing being the opposite, taking an Object and extracting the backing primative.

一个基本的解释是 anint是一种原始数据类型,字面上只是存储在内存中的值。AnInteger是一个 Java 对象,它将 an 包装int在一个类中,其中包含许多不错的/有用的方法,可以调用这些方法来处理int隐藏在内部的支持。这与大多数原始数据类型相同,例如booleanBooleancharCharacter等。这被称为原始数据类型Boxing。拆箱是相反的,取一个对象并提取支持原语。

Here's an example of how one may use Integerto convert a Stringinto 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 Integeris an object, whereas an intis a primitive. So you can have a null reference to an Integerand a Setor Listof them. You can not do that with an int.

AnInteger是一个对象,而 anint是一个原始类型。因此,您可以对它们中的一个Integer和一个Set或一个进行空引用List。你不能用int.