例如,为什么你可以做:

int n = 9;

而不是:

Integer n = 9;

你可以这样做:

Integer.parseInt("1");

而不是:

int.parseInt("1");

Integer基本上只是原始类型int的包装器。它允许您使用Integer类的所有函数,使您的工作更加轻松。

如果您是Java新手,那么您应该学会欣赏Java文档。例如,您想了解的关于Integer类的任何内容都有详细的文档。

这是直接从Integer类的文档中出来的:

Integer类将原始类型int的值包装在对象中。Integer类型的对象包含一个int类型的字段。

Integer是Java中的包装器类型,而int是基本类型。在Java中,除了基本数据类型之外的所有内容都是作为对象实现的,这意味着Java是一种高质量的纯面向对象编程语言。如果需要,所有的原语类型在Java中也可以作为包装器类型。使用基本类型可以获得一些性能上的好处,因此包装器类型应该只在必要时使用。

在下面的例子中。

Integer n = 9;

常量9被自动装箱(自动装箱和解装箱从java 5开始自动发生)为Integer,因此你可以使用这样的语句和Integer n = new Integer(9)。这实际上是通过语句Integer.valueOf(9).intValue();

Int是表示整数的基本类型。而Integer是一个包装int的对象。Integer对象提供了更多的功能,例如转换为十六进制、字符串等。

你也可以在Integer中使用OOP概念。例如,您可以使用Integer作为泛型(即Collection<Integer>)。

Int是基本类型,不是对象。这意味着没有与之关联的方法。Integer是一个具有方法的对象(例如parseInt)。

在更新的java中,有自动装箱(和解装箱)的功能。这意味着编译器将在需要的地方插入Integer.valueOf(int)或integer.intValue()。这意味着它实际上是可以写的

Integer n = 9;

它被解释为

Integer n = Integer.valueOf(9);

Integer是包装器类/对象,int是基本类型。当您想在集合中存储int值时,这种差异发挥了巨大的作用,因为它们只接受对象作为值(直到jdk1.4)。JDK5之后,因为自动装箱,它是一个完全不同的故事。

Int是一个基本类型。int类型的变量存储要表示的整数的实际二进制值。int. parseint("1")没有意义,因为int不是一个类,因此没有任何方法。

Integer是一个类,与Java语言中的其他类没有什么不同。Integer类型的变量存储对Integer对象的引用,就像任何其他引用(对象)类型一样。Integer.parseInt("1")是从类Integer调用静态方法parseInt(注意,该方法实际返回int而不是Integer)。

更具体地说,Integer是一个具有单个int类型字段的类。该类用于需要像对待任何其他对象一样对待int的地方,例如泛型类型或需要可空性的情况。

注意,Java中的每个基本类型都有一个等效的包装器类:

字节有字节 短有短 int有整数 长有长 布尔有布尔 char有字符 浮动有浮动 double有double

包装器类继承自Object类,而原语类则不是。所以它可以在集合中使用对象引用或泛型。

从java 5开始,我们就有了自动装箱,并且基本元素和包装类之间的转换是自动完成的。但是要注意,这可能会引入微妙的bug和性能问题;明确转换永远不会有坏处。

int变量保存一个32位有符号整数值。Integer(大写I)保存对(类)类型Integer或null对象的引用。

Java自动在两者之间进行类型转换;当Integer对象作为int操作符的参数出现,或者被赋值给int变量,或者一个int值被赋值给Integer变量时,从Integer到int。这种类型转换称为装箱/解装箱。

如果一个引用null的Integer变量被显式或隐式地解盒,则抛出NullPointerException异常。

(在上述文本中,“变量”指局部变量、字段或参数)

为了优化Java代码运行时,添加了int基元类型,包括float, bool等,但它们与包装器类一起出现,以便在需要时可以转换并使用它们作为标准Java对象以及作为其成员函数的许多实用程序(如Integer.parseInt("1"))。

在Java中,int是一个基本数据类型,而Integer是一个Helper类,它用于将一种数据类型转换为另一种数据类型。

例如:

         double doubleValue = 156.5d;
         Double doubleObject  = new Double(doubleValue);
         Byte myByteValue = doubleObject.byteValue ();
         String myStringValue = doubleObject.toString();

基本数据类型存储最快的可用内存,而Helper类比较复杂,存储在堆内存中。

参考“David Gassner”Java基本训练。

int是基本数据类型,而Integer是Java中的引用或包装器类型(类)。

在java 1.5之后,引入了自动装箱和拆箱的概念,你可以像这样初始化int或Integer。

int a= 9
Integer a = 9 // both valid After Java 1.5.

为什么Integer.parseInt (" 1 ");但不是int.parseInt("1");??

Integer是jdk库中定义的类,parseInt()是属于Integer Class的静态方法

所以,Integer.parseInt (" 1 ");在java中是可能的。但是int在java中是基本类型(假设是关键字)。所以,你不能用int调用parseInt()。

本文摘自《Java:完整参考,第九版》

Java uses primitive types (also called simple types), such as int or double, to hold the basic data types supported by the language. Primitive types, rather than objects, are used for these quantities for the sake of performance. Using objects for these values would add an unacceptable overhead to even the simplest of calculations. Thus, the primitive types are not part of the object hierarchy, and they do not inherit Object. Despite the performance benefit offered by the primitive types, there are times when you will need an object representation. For example, you can’t pass a primitive type by reference to a method. Also, many of the standard data structures implemented by Java operate on objects, which means that you can’t use these (object specific) data structures to store primitive types. To handle these (and other) situations, Java provides type wrappers, which are classes that encapsulate a primitive type within an object. Wrapper classes relate directly to Java’s autoboxing feature. The type wrappers are Double, Float, Long, Integer, Short, Byte, Character, and Boolean. These classes offer a wide array of methods that allow you to fully integrate the primitive types into Java’s object hierarchy.