File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

range checking

amit mawkin
Ranch Hand

Joined: Oct 31, 2001
Posts: 73

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


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

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.

HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: range checking
 
Similar Threads
Final variables and downcasting
Type conversion
Local Inner Class
Why final constants won't take the default values?
Object Conversion Rules!Pls help me!