aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes final int/byte 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 int/byte" Watch "final int/byte" New topic
Author

final int/byte

suresh kamsa
Ranch Hand

Joined: Jul 30, 2001
Posts: 149
public static void main(String args[]) {

final int i = 10;
byte b = i;
System.out.println("byte is " + b);
final long l = 20l; // line 1
byte b1 = l;
System.out.println("byte is " + b1);
}
Why the error at line 1?
Thanks
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
I get the compiler error on the line after line 1. But this is still a good question -- why does the compiler allow assignment to a byte from a final int, but not from a final long?


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
The error is not at line 1, but at the one below. Because you need to show the compiler you are aware that a loss of precission can ocurr when assigning long to byte.


SCJP2. Please Indent your code using UBB Code
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Ron JLS 5.2 will resolve your doubt

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.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Thanks. This rule is a bit quirky, since there cannot be a loss of precision when converting from a constant final long to a byte (just as there can't when converting from a constant final int to a byte).
[ August 20, 2002: Message edited by: Ron Newman ]
suresh kamsa
Ranch Hand

Joined: Jul 30, 2001
Posts: 149
Its typo mistake and both of you are right. Error will be on line after 1. Why is it not happening when we say final int i =10 and assining it to byte?
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
I think problem is that we are using final
in a method body. final is to be used for data
members defined inside a class. That answers
suresh last question...
[ August 20, 2002: Message edited by: Barkat Mardhani ]
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Actually, Jose already pointed it out

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.

Declaring a variable as final makes its value known at compile-time.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Originally posted by Barkat Mardhani:
I think problem is that we are using final
in a method body. final is to be used for data
members defined inside a class.

It's perfectly legal to declare final local variables.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
It is perfectly legal to use final in a method body.
suresh kamsa
Ranch Hand

Joined: Jul 30, 2001
Posts: 149
Thanks to all, gotcha.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: final int/byte
 
Similar Threads
Assignment doubt
How to pass a long in ...Long ll = Long.valueOf(3L);//gives
Wrapper Class Problem
why I cann't assign FINAL INT to Byte
casting