Regarding doubt about final
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   johny_sk
Posted On:   Friday, December 29, 2006 06:07 AM

The below code works fine when there is final is given for variable k

final int k = 25;
byte b = k;
System.out.println(b);

But after removing final for "k" then it is compile time error.

What is the reason?

Re: Regarding doubt about final

Posted By:   WarnerJan_Veldhuis  
Posted On:   Friday, December 29, 2006 07:09 AM

Because k is an int, per definition, you need to explicitly downcast it into a byte (possible loss of precision is the compile error). But because k is final, the compiler knows that k will definately fit into the byte, because it cannot change.



This is what the java sourcecode is:

public class Test {

public static void main(String[] arg) {
final int k = 25;
byte b = k;
System.out.println(b);

}

}


This is what the compiler created (decompiled):

public class Test
{

public Test()
{
}

public static void main(String args[])
{
byte byte0 = 25;
System.out.println(byte0);
}
}
About | Sitemap | Contact