• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

casting error #2

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you praveen for the explanation.
Can you explain little more on "why final modifier does not give an error?"
 
author
Posts: 23937
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic