• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Compile time and run time question about "switch"

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having problems understanding what the JVM does at runtime and what the compiler does.

This code snippet is from K&B SCP&D for J2 book (p218):



This code won't compile because the case argument must be a compile time constant and apparently "b" is assigned "2" at runtime. In theory a more intelligent compiler would be able to substitute 2 for the final variable b into the bytecode.

Ok, fair enough, the compiler only does the minimum possible and leaves calculations and assignments for the runtime environment.

But then check this code (from same book, Self Test question 4.2):




Based on the first example I would have thought that "x-1" would be evaluated at runtime and consequently this code would cause a compile error.
But no, all of a sudden the compiler isn't a complete doofus and can evaluate "x-1"

So my question is how do you work out what will be done at compile time and what is done at runtime, what rules of thumb are there?

Thanks for any insight.

(ps. I'm studying for the 310-035 exam, should I be posting questions like this here or in the Programmer Certification forum?)
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your first example, b is a blank final variable. It must be definitely assigned a value before it can be used. However, the compiler cannot determine the value at compile-time because the assignment is not made when the variable is declared.

Consider a situation where the blank final variable will be initialized by a value read from the user or from a file. The compiler can't possibly be expected to know the value.

In the second example, since x is a final and its value is known at compile-time, x-1 and x-2 can be computed at compile-time.
[ September 11, 2006: Message edited by: Keith Lynn ]
 
Martin hill
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Keith.

I hadn't considered the possibility that b could be initialised by a value read in from a file.
I just assumed the compiler only had to look through the code to see what value is assigned to b, and if nothing is assigned spit the dummy. It's now starting to make more sense.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Martin hill:
I hadn't considered the possibility that b could be initialised by a value read in from a file.



It doesn't even need to be that complicated. Just consider the following snippet:



Of course the compiler *could* have been written to notice that in your first example, the value actually is fixed. I guess they just decided that it would be too much trouble - and somewhere they *had* to draw the line...
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When final variable is initialized in it's declaration, it is a constant variable (if all other conditions are met), it is is initialized later, it is not. That's the specification.

Arithmetic operations on compile-time constants produce compile-time constants, so x-1 is allowed.

All compilers must obey the Java Language Specification:
JLS - constant expressions
[ September 11, 2006: Message edited by: Vlado Zajac ]
 
Martin hill
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ilja and Vlado, your explanations have cleared up my confusion.

Vlado, that chapter 15 is one nasty chapter! You'd need a PhD in computing to begin to understand the section "Identify Matching Arity Methods Applicable by Subtyping" and a few sections that follow it. My dictionary doesn't even list the word "arity".
 
reply
    Bookmark Topic Watch Topic
  • New Topic