Page 1 of 1

About Question enthuware.ocajp.i.v8.2.1247 :

Posted: Thu Mar 23, 2017 4:03 pm
by java_learner
In the answer to this question it says:
A narrowing primitive conversion may be used if all of the following conditions are satisfied:

1. The expression is a constant expression of type int.
But I tried the following and it worked without problems:

Code: Select all

final char c = 5;
byte b = c;
I also tried

Code: Select all

final short s = 5;
So it seems to me that the constant expression is also allowed to be char or short. It doesn't have to be int.

Am I missing something?

Re: About Question enthuware.ocajp.i.v8.2.1247 :

Posted: Thu Mar 23, 2017 10:11 pm
by admin
You are right. It doesn't have to be an int but can be any integral type i.e. int, byte, char, or short. As long as the value value of the constant expression is representable in the target type of the variable, it can be implicitly narrowed to the target variable.

I have updated the explanation.

thank you for your feedback!
Paul.