For example why can you do:
例如,你为什么可以这样做:
int n = 9;
But not:
而不是:
Integer n = 9;
And you can do:
和你能做什么:
Integer.parseInt("1");
But not:
而不是:
int.parseInt("1");
12 个解决方案
#1
216
int
is a primitive type. Variables of type int
store the actual binary value for the integer you want to represent. int.parseInt("1")
doesn't make sense because int
is not a class and therefore doesn't have any methods.
int是一种原始类型。int类型的变量存储您想要表示的整数的实际二进制值。int. parseint(“1”)没有意义,因为int不是类,因此没有任何方法。
Integer
is a class, no different from any other in the Java language. Variables of type Integer
store references to Integer
objects, just as with any other reference (object) type. Integer.parseInt("1")
is a call to the static method parseInt
from class Integer
(note that this method actually returns an int
and not an Integer
).
Integer是一个类,与Java语言中的其他类没有区别。类型整数存储对整数对象的引用的变量,就像其他引用(对象)类型一样。parseInt(“1”)是对来自类整数的静态方法parseInt的调用(注意,该方法实际上返回一个整数,而不是整数)。
To be more specific, Integer
is a class with a single field of type int
. This class is used where you need an int
to be treated like any other object, such as in generic types or situations where you need nullability.
更具体地说,Integer是一个类型为int类型的类,该类用于需要将int作为其他对象进行处理,例如在泛型类型或需要nullability的情况下。
Note that every primitive type in Java has an equivalent wrapper class:
注意,Java中的每个基本类型都有一个等效的包装类:
-
byte
hasByte
- 字节,字节
-
short
hasShort
- 短有短
-
int
hasInteger
- int有整数
-
long
hasLong
- 长有长
-
boolean
hasBoolean
- 布尔已经布尔
-
char
hasCharacter
- 正如有字符
-
float
hasFloat
- 浮动有浮动
-
double
hasDouble
- 双有双
Wrapper classes inherit from Object class, and primitive don't. So it can be used in collections with Object reference or with Generics.
包装器类继承对象类,而原语则不继承。因此,它可以用于对象引用或泛型的集合中。
Since java 5 we have autoboxing, and the conversion between primitive and wrapper class is done automatically. Beware, however, as this can introduce subtle bugs and performance problems; being explicit about conversions never hurts.
由于java 5是自动装箱的,所以原语和包装类之间的转换是自动完成的。但是,请注意,这可能会带来一些微妙的bug和性能问题;明确转换不会带来伤害。
#2
#3
6
An Integer is pretty much just a wrapper for the primitive type int. It allows you to use all the functions of the Integer class to make life a bit easier for you.
整数对于基本类型int来说只是一个包装器,它允许您使用整数类的所有函数来简化您的生活。
If you're new to Java, something you should learn to appreciate is the Java documentation. For example, anything you want to know about the Integer Class is documented in detail.
如果您是Java新手,您应该学会欣赏的是Java文档。例如,您想知道的关于整数类的任何信息都有详细的记录。
This is straight out of the documentation for the Integer class:
这是关于整数类的文档的直接内容:
The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.
Integer类包装对象中的基本类型int的值。Integer类型的对象包含一个类型为int类型的字段。
#4
5
Integer
refers to wrapper type in Java whereas int
is a primitive type. Everything except primitive data types in Java is implemented just as objects that implies Java is a highly qualified pure object-oriented programming language. If you need, all primitives types are also available as wrapper types in Java. You can have some performance benefit with primitive types, and hence wrapper types should be used only when it is necessary.
Integer指的是Java中的包装器类型,而int是一种原始类型。Java中除了基本数据类型之外的所有东西都被实现,就像Java是一种高度合格的纯面向对象编程语言一样。如果需要,所有原语类型也可以作为Java中的包装器类型使用。您可以对基本类型具有一些性能优势,因此只有在必要时才应该使用包装器类型。
In your example as below.
在下面的例子中。
Integer n = 9;
the constant 9
is being auto-boxed (auto-boxing
and unboxing
occurs automatically from java 5
onwards) to Integer
and therefore you can use the statement like that and also Integer n = new Integer(9)
. This is actually achieved through the statement Integer.valueOf(9).intValue();
常量9是自动装箱(自动装箱和取消装箱自动从java 5开始)到整数,因此您可以使用这样的语句,也可以使用整数n = new Integer(9)。这实际上是通过语句Integer.valueOf(9).intValue()实现的。
#5
3
int is a primitive type and not an object. That means that there are no methods associated with it. Integer is an object with methods (such as parseInt).
int是一种原始类型,而不是对象。这意味着没有相关的方法。Integer是使用方法(例如parseInt)的对象。
With newer java there is functionality for auto boxing (and unboxing). That means that the compiler will insert Integer.valueOf(int) or integer.intValue() where needed. That means that it is actually possible to write
有了更新的java,就有了自动装箱(和取消装箱)的功能。这意味着编译器将在需要的地方插入Integer.valueOf(int)或integer.intValue()。这意味着它实际上是可以写的。
Integer n = 9;
which is interpreted as
这是解释为
Integer n = Integer.valueOf(9);
#6
3
Integer is an wrapper class/Object and int is primitive type. This difference plays huge role when you want to store int values in a collection, because they accept only objects as values (until jdk1.4). JDK5 onwards because of autoboxing it is whole different story.
Integer是一个包装类/对象,int是原始类型。当您希望在集合中存储int值时,这种差异发挥了巨大的作用,因为它们只接受作为值的对象(直到jdk1.4)。因为自动装箱,JDK5的情况完全不同。
#7
2
An int variable holds a 32 bit signed integer value. An Integer (with capital I) holds a reference to an object of (class) type Integer, or to null.
一个int变量持有一个32位的带符号整数值。整数(带有大写I)包含对(类)类型整数的对象的引用,或为null。
Java automatically casts between the two; from Integer to int whenever the Integer object occurs as an argument to an int operator or is assigned to an int variable, or an int value is assigned to an Integer variable. This casting is called boxing/unboxing.
Java在两者之间自动转换;从整数到整数,当整数对象作为一个参数出现在一个int运算符或被赋值给一个int变量时,或者一个int值被赋值给一个整型变量。这种铸造叫做拳击/拳击。
If an Integer variable referencing null is unboxed, explicitly or implicitly, a NullPointerException is thrown.
如果引用null的整型变量是unboxed,显式或隐式的,则抛出NullPointerException。
(In the above text, the term "variable" means local variable, field or parameter)
(在上面的文本中,“变量”是指局部变量、字段或参数)
#8
2
int
is a primitive type that represent an integer. whereas Integer
is an Object that wraps int
. The Integer
object gives you more functionality, such as converting to hex, string, etc.
int是一个表示整数的基本类型。Integer是一个封装int的对象,Integer对象提供了更多的功能,比如转换到十六进制、字符串等。
You can also use OOP concepts with Integer
. For example, you can use Integer for generics (i.e. Collection
).<Integer>
您还可以使用带有整数的OOP概念。例如,您可以使用整数进行泛型(即集合
#9
1
To optimize the Java code runtime, int
primitive type(s) has been added including float
, bool
etc. but they come along with there wrapper classes so that if needed you can convert and use them as standard Java object along with many utility that comes as their member functions (such as Integer.parseInt("1")
).
优化Java代码运行时,int原始类型(s)添加了包括浮动,保龄球等等但他们连同包装类,如果需要你可以转换并使用它们作为标准Java对象以及许多实用程序之际,他们的成员函数(比如Integer.parseInt(" 1 "))。
#10
1
In Java int is a primitive data type while Integer is a Helper class, it is use to convert for one data type to other.
在Java int中是一个原始数据类型,而Integer是一个Helper类,它用于将一个数据类型转换为另一个数据类型。
For example:
例如:
double doubleValue = 156.5d;
Double doubleObject = new Double(doubleValue);
Byte myByteValue = doubleObject.byteValue ();
String myStringValue = doubleObject.toString();
Primitive data types are store the fastest available memory where the Helper class is complex and store in heap memory.
原始数据类型是存储最快可用内存的,其中Helper类是复杂的,存储在堆内存中。
reference from "David Gassner" Java Essential Training.
引用“大卫·加斯纳”Java基本训练。
#11
1
int
is a primitive data type while Integer
is a Reference or Wrapper Type (Class) in Java.
int是一种原始数据类型,而Integer是Java中的引用或包装器类型(类)。
after java 1.5
which introduce the concept of autoboxing and unboxing you can initialize both int
or Integer
like this.
在java 1.5引入了自动装箱和取消装箱的概念之后,您可以像这样初始化整数或整数。
int a= 9
Integer a = 9 // both valid After Java 1.5.
why
Integer.parseInt("1");
but notint.parseInt("1");
??为什么Integer.parseInt(" 1 ");但不是int.parseInt(" 1 ");? ?
Integer
is a Class defined in jdk
library and parseInt()
is a static method belongs to Integer
Class
Integer是在jdk库中定义的类,parseInt()是一个静态方法,属于Integer类。
So, Integer.parseInt("1");
is possible in java. but int
is primitive type (assume like a keyword) in java. So, you can't call parseInt()
with int
.
所以,Integer.parseInt(" 1 ");java中是可能的。但在java中,int是一种基本类型(假定为关键字)。因此,不能用int调用parseInt()。
#12
0
This is taken from Java: The Complete Reference, Ninth Edition
这是取自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.
Java使用基本类型(也称为简单类型),例如int或double,以保存语言支持的基本数据类型。为了性能,这些量使用的是原始类型,而不是对象。对这些值使用对象将会增加一个不可接受的开销,甚至是最简单的计算。因此,原始类型不是对象层次结构的一部分,它们不继承对象。
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.
尽管原始类型提供了性能优势,但有时您需要一个对象表示。例如,您不能通过引用方法传递原始类型。此外,Java实现的许多标准数据结构都对对象进行操作,这意味着您不能使用这些(对象特定的)数据结构来存储基本类型。为了处理这些(和其他)情况,Java提供了类型包装器,它们是封装对象中基本类型的类。
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.
包装类直接关系到Java的自动装箱特性。类型包装器是Double、Float、Long、Integer、Short、Byte、Character和Boolean。这些类提供了大量的方法,使您能够将基本类型完全集成到Java的对象层次结构中。
#1
216
int
is a primitive type. Variables of type int
store the actual binary value for the integer you want to represent. int.parseInt("1")
doesn't make sense because int
is not a class and therefore doesn't have any methods.
int是一种原始类型。int类型的变量存储您想要表示的整数的实际二进制值。int. parseint(“1”)没有意义,因为int不是类,因此没有任何方法。
Integer
is a class, no different from any other in the Java language. Variables of type Integer
store references to Integer
objects, just as with any other reference (object) type. Integer.parseInt("1")
is a call to the static method parseInt
from class Integer
(note that this method actually returns an int
and not an Integer
).
Integer是一个类,与Java语言中的其他类没有区别。类型整数存储对整数对象的引用的变量,就像其他引用(对象)类型一样。parseInt(“1”)是对来自类整数的静态方法parseInt的调用(注意,该方法实际上返回一个整数,而不是整数)。
To be more specific, Integer
is a class with a single field of type int
. This class is used where you need an int
to be treated like any other object, such as in generic types or situations where you need nullability.
更具体地说,Integer是一个类型为int类型的类,该类用于需要将int作为其他对象进行处理,例如在泛型类型或需要nullability的情况下。
Note that every primitive type in Java has an equivalent wrapper class:
注意,Java中的每个基本类型都有一个等效的包装类:
-
byte
hasByte
- 字节,字节
-
short
hasShort
- 短有短
-
int
hasInteger
- int有整数
-
long
hasLong
- 长有长
-
boolean
hasBoolean
- 布尔已经布尔
-
char
hasCharacter
- 正如有字符
-
float
hasFloat
- 浮动有浮动
-
double
hasDouble
- 双有双
Wrapper classes inherit from Object class, and primitive don't. So it can be used in collections with Object reference or with Generics.
包装器类继承对象类,而原语则不继承。因此,它可以用于对象引用或泛型的集合中。
Since java 5 we have autoboxing, and the conversion between primitive and wrapper class is done automatically. Beware, however, as this can introduce subtle bugs and performance problems; being explicit about conversions never hurts.
由于java 5是自动装箱的,所以原语和包装类之间的转换是自动完成的。但是,请注意,这可能会带来一些微妙的bug和性能问题;明确转换不会带来伤害。
#2
31
Integer is a class and int is a primitive type.
Integer是一个类,int是一个原始类型。
Read up on at these links:
阅读这些链接:
#3
6
An Integer is pretty much just a wrapper for the primitive type int. It allows you to use all the functions of the Integer class to make life a bit easier for you.
整数对于基本类型int来说只是一个包装器,它允许您使用整数类的所有函数来简化您的生活。
If you're new to Java, something you should learn to appreciate is the Java documentation. For example, anything you want to know about the Integer Class is documented in detail.
如果您是Java新手,您应该学会欣赏的是Java文档。例如,您想知道的关于整数类的任何信息都有详细的记录。
This is straight out of the documentation for the Integer class:
这是关于整数类的文档的直接内容:
The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.
Integer类包装对象中的基本类型int的值。Integer类型的对象包含一个类型为int类型的字段。
#4
5
Integer
refers to wrapper type in Java whereas int
is a primitive type. Everything except primitive data types in Java is implemented just as objects that implies Java is a highly qualified pure object-oriented programming language. If you need, all primitives types are also available as wrapper types in Java. You can have some performance benefit with primitive types, and hence wrapper types should be used only when it is necessary.
Integer指的是Java中的包装器类型,而int是一种原始类型。Java中除了基本数据类型之外的所有东西都被实现,就像Java是一种高度合格的纯面向对象编程语言一样。如果需要,所有原语类型也可以作为Java中的包装器类型使用。您可以对基本类型具有一些性能优势,因此只有在必要时才应该使用包装器类型。
In your example as below.
在下面的例子中。
Integer n = 9;
the constant 9
is being auto-boxed (auto-boxing
and unboxing
occurs automatically from java 5
onwards) to Integer
and therefore you can use the statement like that and also Integer n = new Integer(9)
. This is actually achieved through the statement Integer.valueOf(9).intValue();
常量9是自动装箱(自动装箱和取消装箱自动从java 5开始)到整数,因此您可以使用这样的语句,也可以使用整数n = new Integer(9)。这实际上是通过语句Integer.valueOf(9).intValue()实现的。
#5
3
int is a primitive type and not an object. That means that there are no methods associated with it. Integer is an object with methods (such as parseInt).
int是一种原始类型,而不是对象。这意味着没有相关的方法。Integer是使用方法(例如parseInt)的对象。
With newer java there is functionality for auto boxing (and unboxing). That means that the compiler will insert Integer.valueOf(int) or integer.intValue() where needed. That means that it is actually possible to write
有了更新的java,就有了自动装箱(和取消装箱)的功能。这意味着编译器将在需要的地方插入Integer.valueOf(int)或integer.intValue()。这意味着它实际上是可以写的。
Integer n = 9;
which is interpreted as
这是解释为
Integer n = Integer.valueOf(9);
#6
3
Integer is an wrapper class/Object and int is primitive type. This difference plays huge role when you want to store int values in a collection, because they accept only objects as values (until jdk1.4). JDK5 onwards because of autoboxing it is whole different story.
Integer是一个包装类/对象,int是原始类型。当您希望在集合中存储int值时,这种差异发挥了巨大的作用,因为它们只接受作为值的对象(直到jdk1.4)。因为自动装箱,JDK5的情况完全不同。
#7
2
An int variable holds a 32 bit signed integer value. An Integer (with capital I) holds a reference to an object of (class) type Integer, or to null.
一个int变量持有一个32位的带符号整数值。整数(带有大写I)包含对(类)类型整数的对象的引用,或为null。
Java automatically casts between the two; from Integer to int whenever the Integer object occurs as an argument to an int operator or is assigned to an int variable, or an int value is assigned to an Integer variable. This casting is called boxing/unboxing.
Java在两者之间自动转换;从整数到整数,当整数对象作为一个参数出现在一个int运算符或被赋值给一个int变量时,或者一个int值被赋值给一个整型变量。这种铸造叫做拳击/拳击。
If an Integer variable referencing null is unboxed, explicitly or implicitly, a NullPointerException is thrown.
如果引用null的整型变量是unboxed,显式或隐式的,则抛出NullPointerException。
(In the above text, the term "variable" means local variable, field or parameter)
(在上面的文本中,“变量”是指局部变量、字段或参数)
#8
2
int
is a primitive type that represent an integer. whereas Integer
is an Object that wraps int
. The Integer
object gives you more functionality, such as converting to hex, string, etc.
int是一个表示整数的基本类型。Integer是一个封装int的对象,Integer对象提供了更多的功能,比如转换到十六进制、字符串等。
You can also use OOP concepts with Integer
. For example, you can use Integer for generics (i.e. Collection
).<Integer>
您还可以使用带有整数的OOP概念。例如,您可以使用整数进行泛型(即集合
#9
1
To optimize the Java code runtime, int
primitive type(s) has been added including float
, bool
etc. but they come along with there wrapper classes so that if needed you can convert and use them as standard Java object along with many utility that comes as their member functions (such as Integer.parseInt("1")
).
优化Java代码运行时,int原始类型(s)添加了包括浮动,保龄球等等但他们连同包装类,如果需要你可以转换并使用它们作为标准Java对象以及许多实用程序之际,他们的成员函数(比如Integer.parseInt(" 1 "))。
#10
1
In Java int is a primitive data type while Integer is a Helper class, it is use to convert for one data type to other.
在Java int中是一个原始数据类型,而Integer是一个Helper类,它用于将一个数据类型转换为另一个数据类型。
For example:
例如:
double doubleValue = 156.5d;
Double doubleObject = new Double(doubleValue);
Byte myByteValue = doubleObject.byteValue ();
String myStringValue = doubleObject.toString();
Primitive data types are store the fastest available memory where the Helper class is complex and store in heap memory.
原始数据类型是存储最快可用内存的,其中Helper类是复杂的,存储在堆内存中。
reference from "David Gassner" Java Essential Training.
引用“大卫·加斯纳”Java基本训练。
#11
1
int
is a primitive data type while Integer
is a Reference or Wrapper Type (Class) in Java.
int是一种原始数据类型,而Integer是Java中的引用或包装器类型(类)。
after java 1.5
which introduce the concept of autoboxing and unboxing you can initialize both int
or Integer
like this.
在java 1.5引入了自动装箱和取消装箱的概念之后,您可以像这样初始化整数或整数。
int a= 9
Integer a = 9 // both valid After Java 1.5.
why
Integer.parseInt("1");
but notint.parseInt("1");
??为什么Integer.parseInt(" 1 ");但不是int.parseInt(" 1 ");? ?
Integer
is a Class defined in jdk
library and parseInt()
is a static method belongs to Integer
Class
Integer是在jdk库中定义的类,parseInt()是一个静态方法,属于Integer类。
So, Integer.parseInt("1");
is possible in java. but int
is primitive type (assume like a keyword) in java. So, you can't call parseInt()
with int
.
所以,Integer.parseInt(" 1 ");java中是可能的。但在java中,int是一种基本类型(假定为关键字)。因此,不能用int调用parseInt()。
#12
0
This is taken from Java: The Complete Reference, Ninth Edition
这是取自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.
Java使用基本类型(也称为简单类型),例如int或double,以保存语言支持的基本数据类型。为了性能,这些量使用的是原始类型,而不是对象。对这些值使用对象将会增加一个不可接受的开销,甚至是最简单的计算。因此,原始类型不是对象层次结构的一部分,它们不继承对象。
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.
尽管原始类型提供了性能优势,但有时您需要一个对象表示。例如,您不能通过引用方法传递原始类型。此外,Java实现的许多标准数据结构都对对象进行操作,这意味着您不能使用这些(对象特定的)数据结构来存储基本类型。为了处理这些(和其他)情况,Java提供了类型包装器,它们是封装对象中基本类型的类。
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.
包装类直接关系到Java的自动装箱特性。类型包装器是Double、Float、Long、Integer、Short、Byte、Character和Boolean。这些类提供了大量的方法,使您能够将基本类型完全集成到Java的对象层次结构中。