wood burning stoves 2.0
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes tipsmart Q25 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "tipsmart Q25" Watch "tipsmart Q25" New topic

tipsmart Q25

Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270
class Q25
public static void main(String []args)
final int i = 100; // line1
byte b = i; // line2
Why does not line 2 generate a compiler error? If I remove the final keyword from line 1, the result is as I expected.
[ October 20, 2003: Message edited by: Cathy Song ]
Thomas De Vos
stable boy
Ranch Hand

Joined: Apr 12, 2003
Posts: 425
The final word means that the actually value of "i" is stored in the byte variable "b". The compiler will replace the declaration with byte b = 100;
Without any consequence the compiler can replace the variable with the value.
Even better when you change your code snippet to:
final int i = 128;
byte b = i;
The compiler flags an error. This is because the value 128 (int) cannot be stored in a byte value.
[ October 11, 2003: Message edited by: Thomas De Vos ]

Try your free <a href="http://www.javacertificate.com" target="_blank" rel="nofollow">SCJP 1.4</a> certification centre.<br />Try your free <a href="http://www.j2eecertificate.com" target="_blank" rel="nofollow">SCWCD</a> certification centre.<br />Try your free <a href="http://www.ejbcertificate.com" target="_blank" rel="nofollow">SCBCD</a> certification centre.<br />Try your <a href="http://www.webspherecertificate.com" target="_blank" rel="nofollow">Websphere (Test 285) </a> certification centre.<br />Try your <a href="http://www.j2mecertificate.com" target="_blank" rel="nofollow">SCMAD</a> certification centre. (New)<br /> <br /><a href="http://blogs.javacertificate.com" target="_blank" rel="nofollow">Java/J2EE Certification Blogging</a>
vinod n.s

Joined: Sep 24, 2003
Posts: 2
When assigning a final variable to a variable, even if the final variable’s data type is wider than the variable, if the value is within the range of the variable an implicit conversion is done.
byte b;
final int a = 10;
b = a; // Legal, since value of ‘a’ is determinable and within range of b
final int x = a;
b = x; // Legal, since value of ‘x’ is determinable and within range of b
int y;
final int z = y;
b = z; // Illegal, since value of ‘z’ is not determinable

[velmurugans study notes]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Why does not line 2 generate a compiler error?

Here is the special assignment conversion rule.
variable = expression
The value of an expression can be assigned to a variable if
1. The expression is a constant expression of type byte, short, char, or int
2. The type of the variable is byte, short, or char
3. The value of the expression (which is known at compile-time, because it is a constant expression) can be represented in the type of the variable
Constant expressions are described in JLS 15.28.
Literals of primitive type are constant expressions.
Simple names that refer to final variables, whose initializers are constant expressions, are constant expressions.
Note: This rule applies not only to variable declarations, but also to return values of methods. This rule does Not apply to arguments of methods.
[ October 11, 2003: Message edited by: Marlene Miller ]
I agree. Here's the link: http://aspose.com/file-tools
subject: tipsmart Q25
It's not a secret anymore!