Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Internal working of Integer class

Tags:

java

bytecode

I wanted to know some internal working of Integer class.

For example we can write:

Integer num = 9;

I wanted to know how an Integerclass is created in this case, as we have not created any Object. How does it works internally?

like image 799
Azuu Avatar asked May 17 '26 01:05

Azuu


2 Answers

This is called autoboxing, a feature introduced in Java 5. The Java compiler transforms your statement into:

Integer num = Integer.valueOf(9);

You can read more about autoboxing here:

  • http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html
  • http://docs.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html
like image 91
Lukas Eder Avatar answered May 18 '26 13:05

Lukas Eder


This concept is called auto-boxing. The compiler will change your code to

Integer num = Integer.valueOf(9);

and continue compiling from there...

like image 44
Mathias Schwarz Avatar answered May 18 '26 13:05

Mathias Schwarz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!