About Question enthuware.ocajp.i.v8.2.1314 :
Moderator: admin
-
- Posts: 7
- Joined: Tue Apr 16, 2019 5:40 pm
- Contact:
About Question enthuware.ocajp.i.v8.2.1314 :
One of the answer is Conversion from byte to short doesn't need a cast. I tried testing this.But it gives compiler error sayin incompatible types
byte by1=10;
s=by1;
error: incompatible types: byte cannot be converted to Short
s=by1;
^
1 error
Thanks,
Veena
byte by1=10;
s=by1;
error: incompatible types: byte cannot be converted to Short
s=by1;
^
1 error
Thanks,
Veena
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1314 :
Please post the complete and exact code that you are trying to compile. From the error message, it looks like you are using Short instead of short.
If you like our products and services, please help us by posting your review here.
-
- Posts: 7
- Joined: Tue Apr 16, 2019 5:40 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1314 :
You are right. It was Short ,not short. Thank you.
-
- Posts: 10
- Joined: Wed Aug 08, 2012 5:00 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1314 :
Conversion from char to long does not need a cast. the answer is true or false?
Its shows true
Its shows true
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1314 :
What happened when you tried it out?
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Sun Feb 07, 2021 9:49 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1314 :
Conversion from int to float does need a cast. The opposite is not true. For instance next will not compile:
float fll = 1;
int int23 = 2;
int23 = fll;
float fll = 1;
int int23 = 2;
int23 = fll;
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1314 :
No, conversion from int to float DOES NOT need a cast. conversion from float to int needs a cast.jeroenisanders wrote: ↑Sun Feb 07, 2021 11:12 amConversion from int to float does need a cast. The opposite is not true. For instance next will not compile:
float fll = 1;
int int23 = 2;
int23 = fll;
That is why, in your example, the first line compiles fine. The third line does not.
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Fri Jun 03, 2022 9:14 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1314 :
"Conversion from short to char needs a cast". The reason given is because their ranges are not compatible. Both are 16 bits though.
1.Is it because char is unicode and short is integral?
2.Notice that byte is 8 bits and yet still need casting to fit into char(16 bits).Why?
1.Is it because char is unicode and short is integral?
2.Notice that byte is 8 bits and yet still need casting to fit into char(16 bits).Why?
Code: Select all
byte bytey =1;
char chary = 'a';
chary = bytey; //fail
chary = (char)bytey; //OK
-
- Site Admin
- Posts: 10055
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1314 :
No, it is because short can have negative values while char cannot. byte, char, short, int , and long are all integral types.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 40 guests