aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A question on 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 "A question on conversion" Watch "A question on conversion" New topic
Author

A question on conversion

Achutha Prasad
Greenhorn

Joined: Jan 25, 2001
Posts: 2
Hi ,

We know that an implicit conversion from an int to an byte is not possible. But when the int variable is declared as final then the implicit conversion takes place. Y is it so? The below code compiles and give an output 100.
public test{
public static void main(String args[])
{
final int i = 100;
byte b = i;
System.out.println(b);
}
}
Son Le
Greenhorn

Joined: Jan 15, 2001
Posts: 21
Achutha,
1.final int i = 100;
2.byte b = i;
3.System.out.println(b);
final keywords has an advantage that is the compiler can inline final elements. Line 2 will be treated like:
2.byte b=100;
Therefore, it's legal.
Hungson Le


-LHS
venkatesan Rajagopalan
Greenhorn

Joined: Jan 12, 2001
Posts: 15
Achutha,
you are right implicit conversion cannot take place from int to byte.
you will agree that when final modifier is given that particular variable value remains constant and cannot be changed, right.
In the instant case, the compiler implicitly knows for sure that the int value will remain pegged to 100 and would never be changed and the compiler upon checking that it is within the range of byte, compiles the program and no casting errors is spewed up.
regards
venkat
Achutha Prasad
Greenhorn

Joined: Jan 25, 2001
Posts: 2
Thanks venkat and Son.
Regards,
prasad
[This message has been edited by Achutha Prasad (edited January 25, 2001).]
 
 
subject: A question on conversion