wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes compile time constants Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "compile time constants" Watch "compile time constants" New topic
Author

compile time constants

Arvind Kini
Ranch Hand

Joined: Dec 28, 2000
Posts: 31
Consider the following two similar program code .
class constant{
public static void main(String str[]){
int i ;
final boolean b = true;
if (b) //b is final so if loop shud execute alwayz!
i = 10;
System.out.println("i = " + i); //gives variable not initialized error
}
}

and

class constant{
public static void main(String str[]){
int i ;
if (true) //compile time constant
i = 10;
System.out.println("i = " + i); //compiles fine .
}
}

In the first block since boolean b is final it shud be treated as a compile time constant and hence
during compilation itself if(b) shud be replaced by if(true) and so shud not give any "variable not initialized error "
Please help
Thanx a lot
arvind
Maitham H
Greenhorn

Joined: Jan 07, 2001
Posts: 29
Arvind
I have tried your program
it does work ok, both the first and second, and I don't get any
errors at all.
make sure you are not missing out anything somewhere.
I used Jbuilder to compile and run the exact same programs.
and in both of them , they compile and run perfectly with no errors.


A Software life cycle can be greater than its Developer's
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Arvind,
Both sets of code compile and run without error under JDK 1.3.

Jane
[This message has been edited by Jane Griscti (edited January 14, 2001).]


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Arvind Kini
Ranch Hand

Joined: Dec 28, 2000
Posts: 31
I am using jdk1.2 . does that matter here?
Please confirm as regards this wrt the scjp2 exam
regards
arv
Arvind Kini
Ranch Hand

Joined: Dec 28, 2000
Posts: 31
cud nt make it run here guys. please help
reghards
arvind
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Arvind,
It seems that you have found a bug in JDK1.2. I have tried your example with JDK1.1, JDK1.2, and JDK1.3. It works in both 1.1 and 1.3, but the error happens with 1.2!
Interesting,
Manfred.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Arvind,
Looks as if Manfred is right ... you found a 'bug'.
There is something very similar reported in the Sun Bug database compile-time constants not used correctly for 'unreachable statements'
It reportedly does not occur in JDK 1.3
Hope that helps
------------------

Jane Griscti
Sun Certified Java 2 Programmer
"When ideas fail, words come in very handy" -- Goethe
bhakti soman
Greenhorn

Joined: Nov 12, 2000
Posts: 22
hi arvind
both the code compile fine with me.i have SDK 1.3

Originally posted by Jane Griscti:
Arvind,
Looks as if Manfred is right ... you found a 'bug'.
There is something very similar reported in the Sun Bug database compile-time constants not used correctly for 'unreachable statements'
It reportedly does not occur in JDK 1.3
Hope that helps

Arvind Kini
Ranch Hand

Joined: Dec 28, 2000
Posts: 31
Thanx a ton guys for gettin rid of my confusion . I wud be appearing for SCJP on sat !
arvind
Cherry Mathew
Ranch Hand

Joined: Dec 26, 2000
Posts: 159
but finals work in while loop try all combinations
try giving final boolean b = true ;
while(b){
}
system.out.println("cherry");
Cherry
Weigang Gu
Ranch Hand

Joined: Jan 16, 2001
Posts: 44
Cherry:
It did not work for while loops in my hands. I use jdk1.2
The question to all of you:
if such a question occurs during the exam, what should we do?

Originally posted by Cherry Mathew:
but finals work in while loop try all combinations
try giving final boolean b = true ;
while(b){
}
system.out.println("cherry");
Cherry

Ishaan Mohan
Ranch Hand

Joined: Jan 20, 2001
Posts: 115
Hi,
if (b)
i = 10;
System.out.println("i = " + i);
In this part of code posted by Arvind there is no brackets for if so compiler takes a precuation assuming that if case is of false then in that case varible i will not be initialize and as it is a local variable so compiler is giving :
Variable i may not have been initialized.
System.out.println("Value of i is " + i);
This error is related to variable i not because of b.
Code posted by Jane whereas have brackets in if body and System.out.println statement is inside that so it compiles sucessfully.
If I understood code incorrectly then please correct me.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Ishaan,
If <code>b</code> wasn't a <code>final</code> variable, you would be right; the braces make a big difference, however, in this instance they don't matter.

The above code compiles without error because <code>b</code> is <code>final</code> and will equal <code>true</code> for the duration.
If you remove the <code>final</code> keyword:

You get a compile error: variable i might not have been initialized.
Hope that helps.

------------------
Jane Griscti
Sun Certified Java 2 Programmer
"When ideas fail, words come in very handy" -- Goethe
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compile time constants