• 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

How to know which statements execute at compile time and which at runtime

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I was going through a book where i found the following code.
final int x=2;
final int y;
y=5;
switch(x)
{
case x:System.out.print("some thing");
break;
case y:System.out.print("some thing");
break;
}
Now when we try to compile it it will give an error, the reason being y is not a compile time constant.
Now my question is how can we differentiate between the statements executed at runtime and statements executed at compile time. Please suggest me some link or some solution.
Thanks in advance
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Final variables need to be initialized on the same line that they are declared. See the following post for more details.

https://coderanch.com/t/403358/java/java/Compile-time-constant
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's not exactly true ...

a) Final members of a class need to be initialized either on the same line they are declared, or by any given constructor.

b) Final variables in a method body can be initialized anytime before the first read access, you just have to make sure they are initialized only once. This can be, for instance, also in an if-elseif-...-elseif-else statement, with each conditional block initializing the valiable exactly once.

It's only for a final variable to act as a case in a switch-case block that it needs to be initialized in the same line as it's declared in. But this is for cases having to be constants. Check out the actual compiler error messages ...
[ July 25, 2008: Message edited by: Guido Sautter ]
 
kranthi chamarthi
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My exact question is ,not regarding the final variables(thats just a code snippet for better understanding of my question).Infact i want to know if there is any way by which we can define which statements will be executed at runtime and which at compile time simply by seeing the code
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by kranthi kumar.chamarthi:
...which statements will be executed at runtime and which at compile time simply by seeing the code

They all execute at runtime. Some errors (syntactical) will be detected at compile time, others (semantic) will not, because they are legal Java even if they do not do what the programmer wishes.

BTW: What IDE are you using. You should use something like Eclipse, which will show you your errors right there in the editor.
 
kranthi chamarthi
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
what Guido Sautter said is correct(since y is not a compile time constant it cant be used as a case value, i know that ,forgot to mention in my first post).But what i really want to know is, is there any way to define which statements will execute at compile time and which at run time simply by seeing the code.
Thanks
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, there is a simple way to tell which statements execute at compile-time. None of them. Everything is compiled (or more pedantically translated) into bytecode, but nothing is executed until runtime.

The compile-time constant is one which the compiler can recognise as having a fixed value. Please do a search on JavaRanch for compile-time constant; there have been at least two other threads about similar subject this week. The official definition of a compile-time constant, which is as near as we can get to answering the question in the title of this thread, is in the Java Language Specification �15.28.
 
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 Campbell Ritchie:
Yes, there is a simple way to tell which statements execute at compile-time. None of them.



Well, it's actually not that simple, as far as I know. If I'm not ways off, some constant expressions, such as

"This " + "text"
42*7

actually get "executed" at compile time - the byte code will only contain the value of the expression, not the expression itself.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:


Well, it's actually not that simple, as far as I know. If I'm not ways off, some constant expressions, such as

"This " + "text"
42*7

actually get "executed" at compile time - the byte code will only contain the value of the expression, not the expression itself.



You aren't way off, this is correct. When I first saw the post, this is what I was thinking of. To test it, you can modify the underlying fields of "This " + "text", and that would actually modify "This Text" as well. The compiler is smart enough to figure this type of thing out.

Garrett
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can actually see this in the disassembled code (using the javap tool).

For example, if we compile the class:


Then disassemble it:



You can see in the main method that 294 is loaded (or pushed) as the integer and the full String is loaded as the String.
[ July 27, 2008: Message edited by: Mark Vedder ]
 
kranthi chamarthi
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So is there any way to figure out the statements which execute at compile time and which execute at run time(Any rules which can define this) simply by seeing the code.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No lines are "executed" at compile-time, but as we have already shown, the compiler can calculate the values of compile-time constants and insert them in the bytecode. The rules about what constitutes a compile-time constant are to be found in the Java Language Specification.
 
reply
    Bookmark Topic Watch Topic
  • New Topic