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 range checking 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 "range checking" Watch "range checking" New topic

range checking

amit mawkin
Ranch Hand

Joined: Oct 31, 2001
Posts: 73

Hi consider the following code
public class AnotherDirtyOne
private final int i =10;
private byte k = i;
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

regards, Amit
coffee drinker and Sun Certified Programmer for Java 2 Platform
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
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.

Valentin Crettaz
Sun Certified Programmer for Java 2 Platform

[Blog] [Blogroll] [My Reviews] My Linked In
I agree. Here's the link:
subject: range checking
It's not a secret anymore!