Why can't int have a null value, but Integer has a null value?

Asked 2 years ago, Updated 2 years ago, 25 views

I have a question while studying the wrapper class.

java

2022-09-21 14:18

2 Answers

The int type variable is the primitive type variable. For this reason, we go to the real value storage place, but the Integertype variable is reference type, which is the address of the object, not the actual value That's why you can have a null.

What is the difference between int vs Integer?

int (Primitive data type (long, float, double, . . .)

Integer (Wrapper class (object)


2022-09-21 14:18

The difference between storing the actual value or the address value that points to the actual value.

I think it would be easy to understand if you look at the image below. Because it's the same object, it doesn't matter if you view the Employee as an Integer.

Basic type

Reference type

Source of the image . The image was taken from the top answer.

The answer is great, but it is English :D


2022-09-21 14:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.