int 是 Java 中的对象吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7700573/
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
Is int an object in Java?
提问by soandos
More precisely, is int a part of the Integer class (a stripped down version or something) or is it something else entirely?
更准确地说, int 是 Integer 类的一部分(精简版或其他东西)还是完全是其他东西?
I am aware that int is a value type and Integer a reference type, but does int inherit from Object anyway?
我知道 int 是一种值类型而 Integer 是一种引用类型,但是 int 是否从 Object 继承?
(I am assuming that in this regard int, long, boolean etc are all similar. int was just chosen for convenience)
(我假设在这方面 int、long、boolean 等都是相似的。只是为了方便而选择 int)
回答by mikek3332002
The basic types in Java are not objects and does not inherit from Object.
Since Java 1.5 introduced allowed auto boxing between int and Integer(and the other types).
Because ints aren't Objects that can't be used as generic type parameters eg the
T
inlist<T>
Java 中的基本类型不是对象,也不是从 Object 继承的。
由于 Java 1.5 引入了允许在 int 和 Integer(以及其他类型)之间自动装箱。
因为 int 不是不能用作泛型类型参数的对象,例如
T
inlist<T>
回答by Ryan Stewart
From "Primitive Data Types": "Primitive types are special data types built into the language; they are not objects created from a class." That, in turn, means that no, int
doesn't inherit from java.lang.Object in any way because only "objects created from a class" do that. Consider:
来自“原始数据类型”:“原始类型是语言中内置的特殊数据类型;它们不是从类创建的对象。” 反过来,这意味着int
不以任何方式从 java.lang.Object 继承,因为只有“从类创建的对象”才能这样做。考虑:
int x = 5;
In order for the thing named x
to inherit from Object, that thing would need to have a type. Note that I'm distinguishing between x
itself and the thing it names. x
has a type, which is int
, but the thing named x
is the value 5, which has no type in and of itself. It's nothing but a sequence of bits that represents the integral value "5". In contrast, consider:
为了让命名的东西x
从 Object 继承,那个东西需要有一个类型。请注意,我正在区分x
它自己和它命名的东西。x
有一个类型,它是int
,但命名的东西x
是值 5,它本身没有类型。它只不过是表示整数值“5”的位序列。相反,请考虑:
java.lang.Number y = new java.lang.Integer(5);
In this case, y
has the type Number, and the thing named y
has the type Integer. The thing named y
is an object. It has a distinct type irrespective of y
or anything else.
在本例中,y
类型为 Number,名称为y
Integer 类型。命名的东西y
是一个对象。不管是y
什么,它都有一个独特的类型。
回答by Racooon
If you talk about Integer:
如果你谈论整数:
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 的字段。
In addition, this class provides several methods for converting an int to a String and a String to an int, as well as other constants and methods useful when dealing with an int.
此外,该类提供了几种将 int 转换为 String 和将 String 转换为 int 的方法,以及在处理 int 时有用的其他常量和方法。
int is not object, its a primitive type.
int 不是对象,它是一种原始类型。
回答by siride
Primitive types aren't objects, but are stored directly in whatever context they are needed. If they need to be treated like an object, they can be boxed in an Integer.
原始类型不是对象,而是直接存储在它们需要的任何上下文中。如果需要像对象一样对待它们,则可以将它们装箱在 Integer 中。