0

Why it's correct:

Long l = new Long(10);

but it's not correct:

Long l2 = 10; 

I understand that int is substituted here, but why is new Long(10) correct?

ernest_k
  • 44,416
  • 5
  • 53
  • 99
EuginK
  • 1
  • 2
    `10` is an `int` (`10L` would be a `long`). And an `int` doesn't get boxed into a `Long`. Boxing `10` to `Integer` doesn't make it a `Long`, because an `Integer` is **not** a `Long`. – ernest_k Nov 05 '21 at 10:19
  • 1
    The constructor of `Long` takes a `long`, which the `int` can be converted to. In the second example, a `int` or the autoboxed `Integer` can not be assigned to a `Long` though. You can use a `long` literal to make it work, use `Long l2 = 10L;`. – f1sh Nov 05 '21 at 10:25

0 Answers0