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.
About Question enthuware.ocajp.i.v7.2.972 :
Moderator: admin
Re: About Question enthuware.ocajp.i.v7.2.972 :
Underlining also the fact that:
It gives a compilation error.
This software plush forum is immense.
Code: Select all
int a = 4;
short b = 10+a;
Thanks in advance.Cannot convert int to short: type mismatch.
This software plush forum is immense.
-
- Site Admin
- Posts: 10404
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.972 :
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
-
- Posts: 7
- Joined: Sat Jan 18, 2014 8:04 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.972 :
It is impossible to answer this question incorrect because the answers are mutually exclusive
-
- Posts: 4
- Joined: Tue May 06, 2014 3:37 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.972 :
I agree, you dont need to think about it.Kipchakbaev wrote:It is impossible to answer this question incorrect because the answers are mutually exclusive
2 answers are correct:
1 - false
2 - false
3 - true
4 - none
So of course only 1 and 2 can be the answers

-
- Posts: 8
- Joined: Mon Jul 29, 2019 11:25 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.972 :
But remember, that
final short s = 10;
char c = s; - will be fine work
final short s = 10;
char c = s; - will be fine work

Who is online
Users browsing this forum: Bing [Bot] and 13 guests