用Java如何实现int? [英] How is int implemented in Java?
问题描述
根据Integer
类上的文档:
Integer类将原始类型int的值包装在对象中.类型为Integer的对象包含一个类型为int的字段.
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.
和int
上的文档:
默认情况下,int数据类型是32位带符号的二进制补码整数,其最小值为
-2^31
,最大值为2^31-1
.
另外,根据此答案:
在Java中,每个变量都有在源代码中声明的类型.类型有两种:引用类型和原始类型.引用类型是对对象的引用.基本类型直接包含值.
In Java, every variable has a type declared in the source code. There are two kinds of types: reference types and primitive types. Reference types are references to objects. Primitive types directly contain values.
所以,我的问题是:Java如何实现int
原语类型? Integer
作为类可以想象创建其对象.但是,Integer
类再次使用int
. int
以什么方式实现到java,以便我们可以使用它并执行其所有算术运算.对此有所了解将很有帮助.
So, my question is : How is the int
primitive type implemented in Java? Integer
being a class can imagine creating its object. However again Integer
class uses int
. In what way is int
implemented to java so that we are allowed to use it and allowed to perform all its arithmetic operations. An insight onto this would be much helpful.
我尝试了许多现有的答案和文章,但是没有找到我的问题的答案,其中包括:
I tried many existing answers and articles, however did not find an answer to my question, that include:
- Primitive Data types
- Class Integer
- Java: Primitive Data types
- Can an int be null in Java?
- Primitive data type
PS:
如果我的问题的任何部分不清楚/不正确,请在评论部分告诉我.
推荐答案
int
and other primitive types are implemented directly by the Java compiler and the JVM. They are not classes. They are value types, not reference types.
编译器会发出字节码,例如iload,iadd和istore,而不会涉及方法分派.
The compiler emits byte codes like iload, iadd, and istore, with no method dispatch involved.
Integer
几乎是一个普通的类.它的实例分配在堆上.每个实例都包含一个int
值.
Integer
is pretty much an ordinary class. Its instances are allocated on the heap. Each instance contains an int
value.
这篇关于用Java如何实现int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!