Forums Register Login

casting error #2

+Pie Number of slices to send: Send

In this code, since a and b are final it works fine and prints -1. but, if I remove modifier final then, i get compiler error asking for casting to byte [(byte)a - b] or declaring c of type int.
I don't understand the difference between this to situations and also the reason.
Please help.
+Pie Number of slices to send: Send
byte, short, int, and long are all primitive integer based datatypes(or integral datatypes), so any mathematical operation would first yield an int which will have to be further cast to a respective type. Since byte and short have lesser range(number of bits) than an int type, any int yielding operation has a chance of data loss and hence you need to "downcast". If you perform the same with long you wouldn't get the compiler error.

You can read about this here - http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html



+Pie Number of slices to send: Send
As for the final keyword part, I can take a safe guess why the compiler doesnt show an error!

I think its due to the fact that final variables are treated as invariants. So when you do byte c = a-b, after compilation, the compiler would've already replaced a-b with actual value. So it'd be byte c = 1, which is a valid statement and does not require casting.
+Pie Number of slices to send: Send
Thank you praveen for the explanation.
Can you explain little more on "why final modifier does not give an error?"
+Pie Number of slices to send: Send
 

Janki Shah wrote:
In this code, since a and b are final it works fine and prints -1. but, if I remove modifier final then, i get compiler error asking for casting to byte [(byte)a - b] or declaring c of type int.
I don't understand the difference between this to situations and also the reason.
Please help.




First, to understand the explanation, you need to understand what is a compile time constant... see one of my previous topics for a full explanation....

https://coderanch.com/t/454384/java/java/compile-time-constant


Second, Java has an interesting special rule regarding compile time constants, and assignments (specifically related to section 5.2, under assignment conversions). Here is an excerpt from the JLS...

In addition, if the expression is a constant expression (ยง15.28) of type byte, short, char or int :

--> A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.
--> A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is :
------> Byte and the value of the constant expression is representable in the type byte.
------> Short and the value of the constant expression is representable in the type short.
------> Character and the value of the constant expression is representable in the type char.

If the type of the expression cannot be converted to the type of the variable by a conversion permitted in an assignment context, then a compile-time error occurs.




Basically, with assignments, implicit casting is done (from int to byte) provided that the value is a compile time constant -- and that the value fits into the range of a byte. Given this, then line 5 and line 6 definitely compiles, as int literals are definitely compile time constants -- and the example has values which fits into a range of a byte.

As for line 7, it will only compile if the expression on the right is a compile time constant expression. And interestingly, if line 5 and line 6 are declared as final, then it is... as explained in the other topic.

Henry
It's a tiny ad only because the water is so cold.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 896 times.
Similar Threads
Help on Dan's exam 1
Casting between int and byte
Cast byte to char
why we do not require explicit casting at line no.3
why I cann't assign FINAL INT to Byte
More...

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