aspose file tools*
The moose likes Beginning Java and the fly likes Constant Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constant " Watch "Constant " New topic
Author

Constant

Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

What is difference between constant and compile time constant and their behaviors ?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7807
    
  21

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

Joined: Apr 04, 2012
Posts: 924
    
    1

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

Joined: Aug 06, 2012
Posts: 333

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7807
    
  21

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

Joined: Apr 04, 2012
Posts: 924
    
    1

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

Joined: Apr 04, 2012
Posts: 924
    
    1

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

Joined: Aug 06, 2012
Posts: 333

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

Joined: Apr 04, 2012
Posts: 924
    
    1

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

Joined: Aug 06, 2012
Posts: 333

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

Joined: Apr 04, 2012
Posts: 924
    
    1

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

Joined: Aug 06, 2012
Posts: 333

Yea,i know this concept.Lack of correct terminology made this doubt.Thanks
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

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

Joined: Mar 17, 2011
Posts: 7807
    
  21

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

Joined: Aug 06, 2012
Posts: 333

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
 
subject: Constant