Reputation: 6114
According to the docs on 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.
and the docs on int
:
By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of
-2^31
and a maximum value of2^31-1
.
also, according to this Answer:
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.
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:
PS:
If any part of my question is unclear/incorrect, kindly let me know in the comments section.
Upvotes: 0
Views: 1813
Reputation: 13467
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.
The compiler emits byte codes like iload, iadd, and istore, with no method dispatch involved.
Integer
is pretty much an ordinary class. Its instances are allocated on the heap. Each instance contains an int
value.
Upvotes: 7