1

As far as I know, Integer is a wrapper class for int. So an object must be created for initializing value of a.

Integer x=new Integer(10);

But Integer x=10; seems to work perfectly. Can someone please explain how?

Vasvi Sood
  • 29
  • 1
  • 5

3 Answers3

3

Thanks to autoboxing.

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing.

See docs https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

Pavel Polivka
  • 858
  • 4
  • 22
2

Autoboxing freely converts between primitives and their wrapper classes in most contexts and is described in JLS, §5.1.7. Boxing Conversion.

Boxing conversion converts expressions of primitive type to corresponding expressions of reference type. Specifically, the following nine conversions are called the boxing conversions: [...]

  • From type int to type Integer

At run time, boxing conversion proceeds as follows:

  • If p is a value of type int, then boxing conversion converts p into a reference r of class and type Integer, such that r.intValue() == p [...]

If the value p being boxed is an integer literal of type int between -128 and 127 inclusive (§3.10.1), or the boolean literal true or false (§3.10.3), or a character literal between '\u0000' and '\u007f' inclusive (§3.10.4), then let a and b be the results of any two boxing conversions of p. It is always the case that a == b.

Polygnome
  • 7,639
  • 2
  • 37
  • 57
1

Auto-boxing

Boxing is the process of wrapping a primitive value as an object of a class of the equivalent type. Unboxing refers to the opposite direction, going from object to primitive.

For example, an int primitive can be boxed as a Integer.

Auto-boxing is the Java compiler doing this wrapping/unwrapping behind the scenes.

Auto-boxing simplifies life for us app programmers. In our daily work we can often think of the primitive and object as being the same. But know that they are not the same. There are some situations where a programmer must be aware of whether a primitive or an object is in hand. Enjoy the convenience of auto-boxing, but do not operate blindly.

Your example code

In your example:

Integer x = 10 ;

…the 10 is parsed by the compiler as a int primitive. Then, via auto-boxing, that primitive is wrapped as an Integer object. The instantiation of the Integer object is happening behind the scenes, transparently. A reference to that object is then assigned to the variable named x.

More info

See the links above to Wikipedia. And see the tutorial by Oracle.

Pure OOP

Understand that Java is not purely object-oriented. If it were, we would have no primitives; we would have only objects. And therefore we would have no boxing.

So why did the designers of Java include primitives, adding this wrinkle of complexity to our lives? To facilitate porting code from C and other C-like languages. Easy porting was a crucial requirement at the time of Java’s invention.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • The only catch I'd add here is, that it still doesn't mean that `int` and `Integer` are same, implying, that whenever you need your field to be serializable, you wouldn't want to have a primitive there. – Giorgi Tsiklauri Aug 04 '20 at 16:46
  • @GiorgiTsiklauri I’ll emphasize that a bit more. – Basil Bourque Aug 04 '20 at 16:50