aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what are compile time constant Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what are compile time constant" Watch "what are compile time constant" New topic
Author

what are compile time constant

amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
hii pls tell me what are compile time constant

and what effect does the "final" keyword has with respect to making variable compilte time constant or not...


Thanks and Regards, Amit Taneja
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
JLS

15.28 Constant Expression
ConstantExpression:
Expression
A compile-time constant expression is an expression denoting a value of
primitive type or a String that is composed using only the following:
� Literals of primitive type and literals of type String
� Casts to primitive types and casts to type String
� The unary operators +, -, ~, and ! (but not ++ or --)
� The multiplicative operators *, /, and %
� The additive operators + and -
� The shift operators <<, >>, and >>>
� The relational operators <, <=, >, and >= (but not instanceof)
� The equality operators == and !=
� The bitwise and logical operators &, ^, and |
� The conditional-and operator && and the conditional-or operator ||
� The ternary conditional operator? :
� Simple names that refer to final variables whose initializers are constant
expressions
� Qualified names of the form TypeName . Identifier that refer to final variables
whose initializers are constant expressions
Compile-time constant expressions are used in case labels in switch statements
(�14.10) and have a special significance for assignment conversion (�5.2).
A compile-time constant expression is always treated as FP-strict (�15.4),
even if it occurs in a context where a non-constant expression would not be considered
to be FP-strict.

Examples of constant expressions:
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
cool thanx


so what else are not compile time constants ...give some eg.

what do u mean when one says..


Simple names that refer to final variables whose initializers are constant expressions
???
written in bole above ?

� Qualified names of the form TypeName . Identifier that refer to final variables
whose initializers are constant expressions
Compile-time constant expressions are used in case labels in switch statements
(�14.10) and have a special significance for assignment conversion (�5.2).
A compile-time constant expression is always treated as FP-strict (�15.4),
even if it occurs in a context where a non-constant expression would not be considered
to be FP-strict.




give eg. of each bullet sentence..

if possible...
thanx in advance..
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
1. Literals of primitive type and literals of type String



� Casts to primitive types and casts to type String



� The unary operators +, -, ~, and ! (but not ++ or --)



� Simple names that refer to final variables whose initializers are constant
expressions



� Qualified names of the form TypeName . Identifier that refer to final variables

Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
This might help.
http://qa.jtiger.org/GetQAndA.action?qids=13


Tony Morris
Java Q&A (FAQ, Trivia)
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
Hi Tony,


why do we get o/p as U.......???

i mean why deos the class loads , why is
static final Object S = "s";
not a constant expression???

thanx
amit
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
Can anyone explain this???


thanx
amit
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
What has the assignment of a variable got to do with the printing out of a literal?


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
I quote myself


A compile-time constant expression is an expression denoting a value of
primitive type or a String that is composed using only the following...

From JLS 15.28


Object cannot be a compile time constant, although behind the scenes is an String. But you know that's a decision the compiler cannot take, it is a polymorphical run-time decision.

[ May 15, 2005: Message edited by: Edwin Dalorzo ]
[ May 15, 2005: Message edited by: Edwin Dalorzo ]
saran sadaiyappan
Ranch Hand

Joined: Dec 23, 2004
Posts: 39
Hi Amit,
The primitives are considered as compile time constants and they are not evaluated at runtime.So when u compile the class itself the value is assigned for the "string" object and a ref is already available when it executes in psvm.

Change the same from primitive type to object creation (new String ("Saran")) and you ll find that they are added at runtime.

Hope this should explain your doubt.
Jas Oberai
Ranch Hand

Joined: Jan 16, 2005
Posts: 231
Hi Friends,
I read the JLS specification over and again,but couldn't get a good understanding of compile time constants,and i see this word appears so frequently in different cae and other questions.I would be obliged,if so one could explain this or give me a link that explains this whole concept in greater details.
Thanks


SCJP 1.4 (88%)<br />SCWCD 1.4 (88%)
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
There is no greater detail. The JLS is the authoritative source. The only time that you can extract further detail is when you look at the reference implementation (i.e. the Sun SDK). A rewording of the detail is almost certainly going to be flawed (a lot of technical books unfortunately do such things, which I hypothesise is the source of a lot of the fallacy within the industry).

The example I provide for you on http://qa.jtiger.org/ is a practical demonstration of the JLS - if you run it under the reference implementation, you can see that detail "in action". Feel free to modify it in any way to assist your learning (the copyright notice is merely to make it clear that it is my own work).
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: what are compile time constant