• 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

compile time constants

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Arvind Kini
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using jdk1.2 . does that matter here?
Please confirm as regards this wrt the scjp2 exam
regards
arv
 
Arvind Kini
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cud nt make it run here guys. please help
reghards
arvind
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx a ton guys for gettin rid of my confusion . I wud be appearing for SCJP on sat !
arvind
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but finals work in while loop try all combinations
try giving final boolean b = true ;
while(b){
}
system.out.println("cherry");
Cherry
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic