This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regarding final

 
Neha Mohit
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Keith ,

that means there is differenece between a compile time constant and a constant.


Neha
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Neha Mohit
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to both of you
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but here i=90; how does this mean that the value is being set at runtime and not at compile time?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic