0

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 of 2^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.

Community
  • 1
  • 1
OBX
  • 6,044
  • 7
  • 33
  • 77
  • It's not clear what your question is. What do you mean by "implemented"? – shmosel Feb 07 '17 at 07:11
  • 3
    It's implemented as 32 contiguous bits in memory. What kind of an answer are you looking for? – Ted Hopp Feb 07 '17 at 07:12
  • @shmosel ie when we are using `Integer` , it's an object of `Integer` class. same way, what are we accessing when we use `int` ? And how is it defined in the java language? – OBX Feb 07 '17 at 07:13
  • @TedHopp is `int` defined as part of any class ? – OBX Feb 07 '17 at 07:14
  • @OBX its not defined as a part of any class instead its definition is in java compiler. – jack jay Feb 07 '17 at 07:16
  • No. Java primitives are completely separate from classes. – Ted Hopp Feb 07 '17 at 07:16
  • @TedHopp That's what I was asking for. An answer on how and where these are implemented. with links for further reading if available ! – OBX Feb 07 '17 at 07:17
  • 2
    `int` and other primitive types are implemented as values on the stack. There are byte code instructions for int load, store, add, ... `Integer` is an ordinary class. Every instance is stored as a reference to a heap node, and that node contains an `int` variable. Is this what you're asking? See the JVM Spec. – Jerry101 Feb 07 '17 at 07:18
  • @Jerry101 - Not just the stack. Java `int` values can also live on the heap (for example, an `int` field in a class, or an element of an `int[]` array). – Ted Hopp Feb 07 '17 at 07:20
  • @TedHopp: I think Jerry101 is aware of this. Notice the: *Every instance is stored as a reference to a heap node, and that node contains an int variable-* . But yes, your `int[]` is another example for this – dingalapadum Feb 07 '17 at 07:23
  • 1
    @OBX Assuming that we answered your question. What would you do with that answer. Or, in other words: why do you need to know this in the first place? You should answer this in your question, so that we can help you. – Roland Illig Feb 07 '17 at 07:34

1 Answers1

7

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.

Jerry101
  • 12,157
  • 5
  • 44
  • 63
  • 1
    I think this is most specific a non-jvm developer can provide. For more details I suggest to dig into an jvm implementation, e.g. open jdk, http://openjdk.java.net/ – Martin Ackermann Feb 07 '17 at 07:52