Forums Register Login

type conversion

+Pie Number of slices to send: Send
can some one plz help me
look at this
byte b=2;
b += 10; //1
b = b+10; //2

1. works fine but 2. does not.

i know that we have to explicitly narrow the conversion in 2. by adding byte on RHS but but how come 1. works as both are one and the same thing.
+Pie Number of slices to send: Send
hi sandeep,

byte b=2;
b += 10; //1
b = b+10; //2

1 and 2 are not same thing.
1 is identical to b=(byte) b+10; works fine.
2 does not compile because b is converted to int (due to + operator) and after adding 10, the result(int) can not be assigned to byte value.
+Pie Number of slices to send: Send
hi Areca ...
b=(byte)b+10;wont work ...will give loss of precision ...i think u mean to say b=(byte)(b+10)..this will work fine .....

thanks & regards

srikanth
+Pie Number of slices to send: Send
ha, i meaned b=(byte)(b+10) you are right.
Thanks
A teeny tiny vulgar attempt to get you to buy our stuff
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 528 times.
Similar Threads
Its the hairy ++ again.
primitive conversion
alleged lost of precision
primitive conversion and assignment
short to byte
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:17:19.