aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes compile time constants in switch statements 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 "compile time constants in switch statements" Watch "compile time constants in switch statements" New topic
Author

compile time constants in switch statements

vianyrajnish rajnish
Ranch Hand

Joined: Apr 22, 2007
Posts: 70
Hi all,

I was reading about the Switch Statements (chapter 5) of SCJP Exam study guide.(Kathy sierra & Bert bates).... there I got struct in Legal expression and switch statements.

can any one please explain me about first the difference between compile time & load time constants.. with few examples
or suggest me some references.

I referred google but ... it is not working as they given in some seach results of google.
These are the things i read & tried then:
example program :

class TestCompileConst
{

public static void main(String args[])
{
final int DAY_CREATED = (int)(System.currentTimeMillis());
switch(DAY_CREATED)
{
case 2000: System.out.println("hjhjdffd");
break;
default : System.out.println("default part");
}
}
}

According to my reading the below variable is load time constant which can't be used in switch , but it is compiling.
final int DAY_CREATED = (int)(System.currentTimeMillis());

can any one please help me this...

Thanks,
Vinay Rajnish
Richard Boren
Ranch Hand

Joined: Mar 01, 2001
Posts: 233
Hi Vinay,

You should re-read K&B�s switch text.

You are right,
final int DAY_CREATED = (int)(System.currentTimeMillis()); is a runtime constant; however, switch(DAY_CREATED) is the switch�s expression. It only needs to evaluate to a char, byte, short, int or an enum�s value that can automatically cast to an int. It does not have to be a compile time constant. Actually you wouldn�t normally want a constant in the expression even though it is legal. As you have already proven.

It is the switch�s �case:� that must be a compile time constant and must evaluate to the same type as the switch's expression can use.

Try this:




Richard
vianyrajnish rajnish
Ranch Hand

Joined: Apr 22, 2007
Posts: 70
Hi Richard,
Thanks for the reply, Richard.

I read k&B swith statements... I understood that it is the case argument must be compile constants.
but i have not clear with the differences bewtween load time & compile time constatnts.

lets discuss this example :

class Test {
public static void main(String args[]) {
final int a = 1 ; // compile time constant
final int b ; // runtime constant
b = 1 ;
int x = 1 ;
switch(x) {
case a: ; // okay
case b: ; // error: constant expression required
}
}

In the example above.. here,
final int b ;
b = 1;
variable b has been assigned value of 1, at compile time itself .
But , how come it become runtime constant.
Please explain me a bit and provide some reference to read.

Thanks
Vinay Rajnish
swarna dasa
Ranch Hand

Joined: Mar 15, 2007
Posts: 108
Scenarios possible:-

1)
final int b;
b=1;

2)
final int b ; // runtime constant
int i=0;
b= i;

3)
method (int i) {
final int b;
b=i;
}

Since b=1; is allowed b=i; should be allowed as well, but as we can see in scenario 3, its a runtime constant.

So after final int b; the compiler really doesn't know whats gonna follow.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi,

I take compile time constant in the following way:

If a final primitive is initialized where it is declared and right side of the = operator is compile time constant, then we call it compile time constant, because at compile time we have info what exact value the
primitive variable is holding


eg. final int a=40; // here 40 is constant, so a is compile time constant


If a final primitive first declared and then initialized at any other place
(not while declaring), it is run time constant, because at compile time we don't know what value the primitive variable is holding. Assignment operator
= works at run time in this case.


int time = (int)System.currentTimemillies(); //run time constant

We don't know what is the value of System.currentTimemillies() at compile time, so assignment is delayed till run time.

Same applies to constant reference also, but constant wrapper is not allowed in the case statement;


Some other scenarios regarding compile time constant:

byte b1=10; //ok
int i1=15;
byte b2=i1; //Error

final i2 =20;
byte b3 = i2; //OK

final i3;
i3=12;
byte b4 =i3; //Error


Thanks,
[ May 03, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
Richard Boren
Ranch Hand

Joined: Mar 01, 2001
Posts: 233
Hi Vinay,

final int a = 1 ; // This is a compile time constant because "a" is declared and initialized at the same time so the compiler knows its value.

final int b ; // here "b" is declared, but not initialized so the compiler doesn't know its value.

b = 1 ; // The assignment of "b" value is not known until runtime because the value could change. You could do something like this �b = (someExpression) ? 1 : 2 ;� The value of "b" cannot be known by the compiler.

Hope this helps.

Richard
vianyrajnish rajnish
Ranch Hand

Joined: Apr 22, 2007
Posts: 70
Hi Richar,Chandra Bhat, Swarna Das

Thanks for your replies. Thanks a lot.
Now i have got cleared with my doubt .

Thanks
Vinay Rajnish
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compile time constants in switch statements