This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
I cannot understand why this code compiles and executes fine, ----------------------------------------------------------------------- class Test { public static void main(String []arg) { final int i = 90; byte b = i ; System.out.println(i); } }
----------------------------------------------------------------------- but this doesn't
class Test { public static void main(String []arg) { final int i; i= 90; byte b = i ; System.out.println(i); } }
though in both the cases i is constant. the only differenece in the two codes is
in the first case
final int i = 90; byte b = i ;
in the second case final int i; i= 90; byte b = i ;
Thanks in advance [ April 30, 2006: Message edited by: Neha Mohit ]
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
posted
0
In the second code, i is a constant, but not a compile-time constant.
You can only assign an int to a byte in an assignment if the value of the int is known at compile-time and it is compatible with byte.
Neha Mohit
Ranch Hand
Joined: Apr 25, 2006
Posts: 87
posted
0
Thanks Keith ,
that means there is differenece between a compile time constant and a constant.
Neha
Jeroen T Wenting
Ranch Hand
Joined: Apr 21, 2006
Posts: 1847
posted
0
yes there is. A compile time constant is a constant that gets set at compilation. A runtime constant gets set at runtime.
The second case is a runtime constant. Its value could be anything, typically it might be the ip address of the machine you're running on for example, determined at application startup, or some other system setting. That's a value that doesn't change while the application is running, but can't be determined on compilation so it's determined once when first needed (say in a static initialiser block) and then just used.
42
Neha Mohit
Ranch Hand
Joined: Apr 25, 2006
Posts: 87
posted
0
thanks to both of you
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
Also, I would say that while compile-time constant expression is a fairly well-defined term, "constant" by itself is not. In Java, at least. Often when people say "constant" they mean a compile-time constant expression. However some other people simply mean a final variable (preferably of primitive or immutable type), which may or may not be a compile-time constant. The JLS has never given a general definition of "constant". Thus, whenever you see the term, you should be a bit careful to check: what does the person using the term really mean?
"I'm not back." - Bill Harding, Twister
Ankur kothari
Ranch Hand
Joined: Sep 06, 2009
Posts: 531
posted
0
but here i=90; how does this mean that the value is being set at runtime and not at compile time?
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
4
posted
0
If the value is set at compile-time, you will probably get the compiler using 90 instead of i throughout. Have a look at the method's bytecode with the javap -c instructions. In the second example, the bytecode uses "i". The compiler cannot follow the execution through the whole method; that would be too complicated to program.