• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

range checking

 
amit mawkin
Ranch Hand
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi consider the following code
<center><code>
public class AnotherDirtyOne
{
private final int i =10;
private byte k = i;
}
</code><center>
if we declare a var of a primitive type final
and assign that var to another var of another primitive type having narrower range than the final declared one
Does the compiler checks the value inside the final variable
while compiling
in above case if i=255 compiler generates an error
My question is what happens between say coversion from float to int and from double to say whatever
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From JLS 5.2

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

HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic