• 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

what are compile time constant

 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This might help.
http://qa.jtiger.org/GetQAndA.action?qids=13
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain this???


thanx
amit
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What has the assignment of a variable got to do with the printing out of a literal?
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic