Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When you declare Integer i = 9 in java, is i considered to be primitive type due to autoboxing?

When declaring this in a class:

Integer i = 9;

It complies now due to autoboxing I believe, is i considered primitive data type?

like image 948
OPK Avatar asked Nov 29 '22 07:11

OPK


1 Answers

No, the type of i is still Integer (the reference type) - that's how it's declared, after all. The fact that it happens to be initialized using an int is entirely separate from the type of the variable. The literal 9 is a value of type int, but it's boxed into an Integer.

The code is equivalent to:

Integer i = Integer.valueOf(9);
like image 189
Jon Skeet Avatar answered Dec 15 '22 15:12

Jon Skeet