This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills: The software developer's life manual and have John Sonmez on-line!
See this thread for details.
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final int/byte

 
suresh kamsa
Ranch Hand
Posts: 149
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 149
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 787
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1055
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1055
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is perfectly legal to use final in a method body.
 
suresh kamsa
Ranch Hand
Posts: 149
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all, gotcha.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic