This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes use of final and output of this question? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "use of final and output of this question?" Watch "use of final and output of this question?" New topic
Author

use of final and output of this question?

Anto Telvin
Ranch Hand

Joined: Aug 12, 2008
Posts: 113
hi all
i have the following code
byte a;
final int b=20;
a=b;
System.out.println(a);

here we are assigning an integer directly to the byte variable .but it is not creating any errors .but if i remove that final then it shows.i want to know what effect final keyword makes here


Anto Telvin Mathew<br />Many of the life failures are people who did not realize how close they were to success when they give up. EDISON
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3825

here we are assigning an integer directly to the byte variable


You can as long as it fits in to the size of the byte (-128 to 127). By making variable b final (i.e: you can't modify the value once assigned), compiler can make sure whether the value can fit in to a byte. Hope you got it
[ October 10, 2008: Message edited by: Vijitha Kumara ]

SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Have you read the documentation on the final keyword? One random link is here

Also are you saying that if you remove the final it causes an error?


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Anto Telvin
Ranch Hand

Joined: Aug 12, 2008
Posts: 113
thank you very much
 
Don't get me started about those stupid light bulbs.
 
subject: use of final and output of this question?