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 Type Conversion 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 "Type Conversion" Watch "Type Conversion" New topic
Author

Type Conversion

Salima Lalani
Ranch Hand

Joined: Aug 15, 2002
Posts: 59
Hi,
This eg. is from khalid Mughal.
int i = -20;
final int j = 20;
byte b1 = j;//final value of j in range.No cast required
byte b2 = i; //Compiler Error. Cast required.
If i declare int j without 'final' keyword then compiler will ask for casting otherwise not.
Could somebody help me to understand this.
Thanks in advance,
Salima
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
A narrowing primitive conversion (like int to byte) may be used if the value of the expression is known at compile time, and is representable in the type of the variable. Declaring it as final effectively makes the value known at compile-time. I think there are also a lot of threads on this topic.
Larry Lecomte
Ranch Hand

Joined: Jun 14, 2002
Posts: 37
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.
Salima Lalani
Ranch Hand

Joined: Aug 15, 2002
Posts: 59
Thanks,
Where can i find more on this topic?
Reply
Salima
suresh kamsa
Ranch Hand

Joined: Jul 30, 2001
Posts: 149
check this utl
http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#184206
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Type Conversion