yapkm01
yapkm01

Reputation: 3773

Java: JLS section 5.2 Assignment conversion

This is a follow up with my previous question (which i didn't get any response). Here it goes.

If i'm following strictly the rules as stipulated in JLS section 5.2, the below should have failed.

Byte b = 2;

It should have fail since there are 2 conversion going on here. First the implicit narrowing conversion from int to byte and the autoboxing byte to Byte. It's performing 2 conversion here.

So why didn't it fail?

Rules stipulated in JLS section 5.2 does not allow 2 conversion here.

Upvotes: 0

Views: 117

Answers (1)

Rohit Jain
Rohit Jain

Reputation: 213261

This the quote from the same JLS Section:

A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is:

  • Byte and the value of the constant expression is representable in the type byte.

which clearly applies here.

Upvotes: 3

Related Questions