This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constant

 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is difference between constant and compile time constant and their behaviors ?
 
Winston Gutkowski
Bartender
Pie
Posts: 9490
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ankita modi. wrote:What is difference between constant and compile time constant and their behaviors ?

A constant is any value that cannot be changed, and is usually signified by the word final.

A compile-time constant is a constant that is set at compile time, and mostly they're literals, so:
2
2.0
0L
5.56F
0xFF

and probably many others.

Before version 6, String literals (eg, "Hello") weren't allowed in switch statements, but now they are.
To be honest I'm not sure if that means that they're now regarded as compile-time constants or not, but a look at the JLS will tell you.

Winston
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

consider the below case



here i is a constant but NOT compile time constant. as the name suggest compile time constant is the one which is a CONSTANT and which is KNOWN at Compile time. in above case it is simply a constant , but not known at compile time by the compiler. it is only when the program runs , does the value of i get initialized and which thereafter remains constant. on the other hand if you have something like this

final int i = 7; // now 7 is the compile time constant expression. infact you already know that all literals are known at compile time. a better example would be

final int a =8;
final int b = 7+9;
finat int c = a*b;

in the above case, a b and c all are compile time constants variables. why ? because they are INITIALIZED with compile time constant expression. for more information refer Henry's post at http://www.coderanch.com/t/454384/java/java/compile-time-constant
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Winston Gutkowski
Bartender
Pie
Posts: 9490
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpeet singh wrote:for more information refer Henry's post at http://www.coderanch.com/t/454384/java/java/compile-time-constant

Cheers for that. I'd lost it. And thank you Henry.

Winston
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ankita modi. wrote:


i assume you want to say

final int i;
i = 0;

and

final int j = 0;

i is NOT compile time constant variable , since it is not initialized on the same line , whereas j is a compile time constant variable.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
gurpeet singh wrote:for more information refer Henry's post at http://www.coderanch.com/t/454384/java/java/compile-time-constant

Cheers for that. I'd lost it. And thank you Henry.

Winston


thanks. i had bookmarked that link the first time i came across it. in addition to javadocs, jls i must say that henry's post should be in the aresenal
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpeet singh wrote:
ankita modi. wrote:


i assume you want to say

final int i;
i = 0;

and

final int j = 0;

i is NOT compile time constant variable , since it is not initialized on the same line , whereas j is a compile time constant variable.


I got confused here in your terminology, either it can be constant or variable how both ?
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
did you read Henry's post ?

there are 2 things actually, compile time constant expression and compile time constant variable. compile time constant variable is the one which is declared to be final and initialized ON THE SAME LINE with COMPILE TIME CONSTANT EXPRESSION. now what is compile time constant expression ? literal definitely are . but for complete rules , refer the link i gave in my earlier post.
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpeet singh wrote:did you read Henry's post ?

there are 2 things actually, compile time constant expression and compile time constant variable. compile time constant variable is the one which is declared to be final and initialized ON THE SAME LINE with COMPILE TIME CONSTANT EXPRESSION. now what is compile time constant expression ? literal definitely are . but for complete rules , refer the link i gave in my earlier post.


Okay nice,as now i am aware

final i=0;
//called compile time constant

final j;
j=0;
//called constant variable

Is there anything else called plain constant?.. if yes, then please tell me.

 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes you are right. j is a constant variable but it is not COMPILE TIME CONSTANT VARIABLE since it is not initialized on the same line. i don't think so there is any thing called plain constants in java as per JLS.
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea,i know this concept.Lack of correct terminology made this doubt.Thanks
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpeet singh wrote:yes you are right. j is a constant variable but it is not COMPILE TIME CONSTANT VARIABLE since it is not initialized on the same line. i don't think so there is any thing called plain constants in java as per JLS.



ugh..i know COMPILE TIME CONSTANT but now what is COMPILE TIME CONSTANT VARIABLE ?
 
Winston Gutkowski
Bartender
Pie
Posts: 9490
50
Eclipse IDE Hibernate Ubuntu
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ankita modi. wrote:ugh..i know COMPILE TIME CONSTANT but now what is COMPILE TIME CONSTANT VARIABLE ?

Did you read gurpeet's link? It really does explain pretty much everything you'll ever need to know about compile time constants, variable or otherwise.

Personally, I think that the term is an oxymoron and should never have been used; but I sure ain't interested in writing a JLS.

Winston
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
ankita modi. wrote:ugh..i know COMPILE TIME CONSTANT but now what is COMPILE TIME CONSTANT VARIABLE ?

Did you read gurpeet's link? It really does explain pretty much everything you'll ever need to know about compile time constants, variable or otherwise.

Personally, I think that the term is an oxymoron and should never have been used; but I sure ain't interested in writing a JLS.

Winston


Thanks, i thought i got it, but now i have to read that.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic