Page 1 of 1

About Question enthuware.ocajp.i.v7.2.972 :

Posted: Sat Mar 02, 2013 9:17 am
by The_Nick
Ok, I agree here.
Besides the scope of the question..
short s = 10;
it compiles ok..
10 is implicitly an int.
How does the compile do in order to accomplish the operation without causing an exception?
There is an implicit conversion like (short)10. or the compiler sees at compilation time that the number 10 can actually fit a short and therefore no problem?
What happens behind the scenes?
Thanks a lot in advance.

Re: About Question enthuware.ocajp.i.v7.2.972 :

Posted: Sat Mar 02, 2013 9:20 am
by The_Nick
Underlining also the fact that:

Code: Select all

int a = 4;
short b = 10+a;
It gives a compilation error.
Cannot convert int to short: type mismatch.
Thanks in advance.
This software plush forum is immense.

Re: About Question enthuware.ocajp.i.v7.2.972 :

Posted: Sat Mar 02, 2013 10:15 am
by admin
This is because of implicit narrowing of compile time constant int value into a shorter type such as a byte or short. Check this out for more info: http://stackoverflow.com/questions/2720 ... nd-casting

Re: About Question enthuware.ocajp.i.v7.2.972 :

Posted: Fri Jan 24, 2014 9:17 am
by Kipchakbaev
It is impossible to answer this question incorrect because the answers are mutually exclusive

Re: About Question enthuware.ocajp.i.v7.2.972 :

Posted: Mon Jun 16, 2014 2:34 pm
by wokingtown11
Kipchakbaev wrote:It is impossible to answer this question incorrect because the answers are mutually exclusive
I agree, you dont need to think about it.

2 answers are correct:

1 - false
2 - false
3 - true
4 - none

So of course only 1 and 2 can be the answers :-)

Re: About Question enthuware.ocajp.i.v7.2.972 :

Posted: Tue Oct 01, 2019 7:28 am
by Mikhail Volkov
But remember, that
final short s = 10;
char c = s; - will be fine work :!: