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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Rule about final?" Watch "Rule about final?" New topic
Author

Rule about final?

Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370

class MyClass
{
public static void main(String []args)
{
final int i = 100;
byte b = i;
System.out.println(b);
}
}
why does this code compile and prints 100 while -

class MyClass
{
public static void main(String []args)
{
int i = 100;
byte b = i;
System.out.println(b);
}
}

this code gives compilation error. i mean the first block of code should also give compilation error because it's an assignment of 32 bit int into 8 bit byte?

-- Ashik
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
These are the JLS rules for a permitted narrowing integer conversion:
a narrowing primitive conversion may be used if all of the following conditions are satisfied:

* The expression is a constant expression of type byte, short, char or int.
* The type of the variable is byte, short, or char.
* The value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable.

That is why "final int i = 100;" makes "byte b = i;" legal
but "int i = 100;" does not. A final variable is a constant expression, but a non-final variable is not.


Mike Gershman
SCJP 1.4, SCWCD in process
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi mike
i reading K&T.
is it necessary to read the JLS as most of the explanation discussed in forums are from JLS..
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Because i is a constant expression in one, and not in the other.
JLS 15.28 if I remember rightly.


Tony Morris
Java Q&A (FAQ, Trivia)
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
i reading K&T.
is it necessary to read the JLS as most of the explanation discussed in forums are from JLS.


First of all, many of the SCJP mock exams ask questions a litle harder than the actual SCJP exam. Since K&B know what is in the exam, they only cover exam topics. So many JavaRanch questions are not fully answered in K&B.

Almost all Java questions are answered in the JLS and the API documentation, and this material is correct by definition. If the real SDK behaves differently, that is a bug (I know one JavaRancher who disagrees with this). In addition, working Java programmers need to know how to use this material easily. Finally, the stuff is available for free at the Sun Java web site, so everyone has a copy. That is why we often answer questions by quoting the JLS and API doc.
[ March 23, 2005: Message edited by: Mike Gershman ]
sabeer
Ranch Hand

Joined: Mar 15, 2005
Posts: 41
The value of final variable cannot be changed during the execution. So,
final int i=100; i always have value 100.
byte b=i is legal since 100 is fit to range of byte. But
final int i=129;
byte b=i is illegal.

(2) int i=100;
byte b=i is legal because the value of i can be changed during the program execution.
sabeer
Ranch Hand

Joined: Mar 15, 2005
Posts: 41
Sorry, please ignore pt(2) on my previous post. It should be like this

(2) int i=100;
byte b=i is illegal because the value of i can be changed during the program execution.
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Sabeer Sabeer, Change your display name before a bartender catches you !!


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
int i=100;
byte b=i;
is illegal because the value of i can be changed during the program execution.

A better way to say this that i is not a compile-time constant.
Consider a blank final variable:


i cannot be changed after it is initialized in the constructor, but i is not a compile-time constant so a narrowing primitive conversion is not allowed.
[ March 24, 2005: Message edited by: Mike Gershman ]
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
Originally posted by Mike Gershman:


i cannot be changed after it is initialized in the constructor, but i is not a compile-time constant so a narrowing primitive conversion is not allowed.

[ March 24, 2005: Message edited by: Mike Gershman ]




Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Thanks, Jay.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Rule about final?