GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt with the final key 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 "Doubt with the final key" Watch "Doubt with the final key" New topic
Author

Doubt with the final key

Kitty Dayal
Ranch Hand

Joined: Jul 22, 2004
Posts: 89
I couldn't make difference between the two snippet's below



The first one compiles out, while other runs fine. The only diff is using the final key in the second. This is from dan's questions and he does not mention anything on the final key.

Thanx.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Kitty,

Thank you for using my exam.

The first code fragment is as follows.

char c1 = 1;
byte a = c1;

In the first case, the code attempts to assign a char value to a byte variable. The char is a 16 bit unsigned type while byte is an 8 bit signed type. As you can imagine, there are a lot of char values that can not be represented by a variable of type byte.

The second code fragment is as follows.

final char c1 = 1;
byte a = c1;

In the second case, the "final" modifier is added to the declaration of the char variable. As a result, the char variable is now a compile-time constant. The compiler recognizes that the constant value, 1, can be assigned to a variable of type byte.

This is covered in
Section 5.2 of the Java Language Specification.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Kitty Dayal
Ranch Hand

Joined: Jul 22, 2004
Posts: 89
Thanks Dan.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt with the final key