aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes casting error #2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "casting error #2" Watch "casting error #2" New topic
Author

casting error #2

Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136

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.
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
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



Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
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.
Janki Shah
Ranch Hand

Joined: Nov 23, 2011
Posts: 136
Thank you praveen for the explanation.
Can you explain little more on "why final modifier does not give an error?"
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18992
    
  40

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....

http://www.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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: casting error #2