File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes final long to byte ..why not...? 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 "final long to byte ..why not...?" Watch "final long to byte ..why not...?" New topic

final long to byte ..why not...?

Prashant Neginahal
Ranch Hand

Joined: Dec 04, 2002
Posts: 76
Hi All,
i have one doubt ,
final int x=10;
byte b=x; --> is possible.But,
final long m=10;
byte b1=m; --> Gives error.Why ? can anybody explain me.?

--------------<br />Prashant<br />SCJP-91%
Hemal Mehta
Ranch Hand

Joined: Nov 16, 2000
Posts: 101
In the case of int it allows this value because the byte can accomodate a +ve value upto 127. Increase the value more than 127 and it will give you compilation error.
In case of long value it will complain of posible loss of precision as you are trying to convert a 64 bit long to a byte. Let me know if you still need clarification
Sarma Lolla
Ranch Hand

Joined: Oct 21, 2002
Posts: 203
Prashant Neginahal,
All these days I thought that a final long can be assigned to byte. But it seems it is not the case. Even a final long can't be assigned to int.
Dan Culache
Ranch Hand

Joined: Jan 24, 2003
Posts: 70
Sarma, you are right, I believe this is an inconsistency in the compiler behavior.
The compiler should check that the final "variable" is within the byte range and allow the assignment or not. It shouldn't matter if it's an int or a long, they are both way wider than byte anyway.
Actually I thought that the compiler is replacing every occurrence of a final field with its value.
Ambapali Pal
Ranch Hand

Joined: Dec 17, 2002
Posts: 47
Yeah, I dodnot know why but where ever only int type is allowed, it does not take any long even though it is final.
For another example switch statement which only allow int or anything which can be implicitly promoted to int.

int i = 1;
final long b = 1;
case 2:// something
case 1://something
None of them compiles.
So I just take it granted that where only int is allowed, no final long or no long literal is allowed.
Hope I am correct.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
To know exactly what is allowed JLS 5.2

5.2 Assignment Conversion
Assignment conversion occurs when the value of an expression is assigned (�15.26) to a variable: the type of the expression must be converted to the type of the variable. Assignment contexts allow the use of an identity conversion (�5.1.1), a widening primitive conversion (�5.1.2), or a widening reference conversion (�5.1.4). In addition, a narrowing primitive conversion may be used if all of the following conditions are satisfied:
* The expression is a constant expression of type byte, short, char or int.
* The type of the variable is byte, short, or char.
* The value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable.
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.

SCJP2. Please Indent your code using UBB Code
I agree. Here's the link:
subject: final long to byte ..why not...?
jQuery in Action, 3rd edition