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
Online
-
- Site Admin
- Posts: 10388
- 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.
-
- 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
Online
-
- Site Admin
- Posts: 10388
- 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?
-
- Posts: 5
- 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;
Online
That is why, in your example, the first line compiles fine. The third line does not.
-
- Site Admin
- Posts: 10388
- 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.
-
- 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
Online
-
- Site Admin
- Posts: 10388
- 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.
Who is online
Users browsing this forum: Google [Bot] and 8 guests