aspose file tools*
The moose likes Java in General and the fly likes How to know which statements execute at compile time and which at runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to know which statements execute at compile time and which at runtime" Watch "How to know which statements execute at compile time and which at runtime" New topic
Author

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

kranthi chamarthi
Ranch Hand

Joined: May 08, 2007
Posts: 95
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
Bill Cruise
Ranch Hand

Joined: Jun 01, 2007
Posts: 148
Final variables need to be initialized on the same line that they are declared. See the following post for more details.

http://www.coderanch.com/t/403358/java/java/Compile-time-constant
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
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

Joined: May 08, 2007
Posts: 95
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
Darius Cooper
Greenhorn

Joined: Jul 10, 2008
Posts: 8
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

Joined: May 08, 2007
Posts: 95
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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Garrett OBrien
Greenhorn

Joined: Oct 25, 2005
Posts: 8
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
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

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

Joined: May 08, 2007
Posts: 95
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
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to know which statements execute at compile time and which at runtime