[668] in java-interest

home help back first fref pref prev next nref lref last post

Re: Java compiler is trying too hard for me

daemon@ATHENA.MIT.EDU (Arthur van Hoff)
Wed Jul 12 17:42:04 1995

Date: Wed, 12 Jul 1995 13:15:11 -0800 (PDT)
From: Arthur van Hoff <Arthur.Vanhoff@Eng.Sun.COM>
Reply-To: Arthur van Hoff <Arthur.Vanhoff@Eng.Sun.COM>
To: Tientien Li <li@deming.Jpl.Nasa.Gov>
Cc: java-interest@java.Eng.Sun.COM


Hi Tientien,

> > > The Java compiler "javac" won't let me do the following type of code:
> > > 
> > >    class smartjavac {
> > >       public void init() {
> > >          int x;
> > >          for (int i=0; i<3; i++) {
> > >             if (i == 0)  x = i;
> > >             else if (i == 1)  x = i;
> > >             else x = i;
> > >          }
> > >          System.out.println("ValueOfX is " + x );
> > >       }
> > >    }
> > 
> > Try giving x an initial value.  The compiler may think that it is possible
> > that the loop may be executed 0 times, since compile-time determination
> > is not always possible.
> > 
> > Dan Evans
> > dan.evans@peri.com
> >
> 
> Dan,
> 
> OK, I agree with you its a good idea to initialize variables. From now on, 
> I'll try to init all local variable to make "javac" happy. However, this 
> problem is a bit more complicated, e.g., the "javac" will let the following 
> code thru:
> 
>    class smartjavac {
>       int x;    // x is now moved to outside....
>       public void init() {
>          for (int i=0; i<3; i++) {
>             if (i == 0)  x = i;
>             else if (i == 1)  x = i;
>             else x = i;
>          }
>          System.out.println("ValueOfX is " + x );
>       }
>    }
> 
> Looks like "javac" only check this condition for variables defined locally
> within the current function scope.

Instance variables are automatically initialized to 0 (or the equivalent
thereof).

Have fun,

	Arthur van Hoff
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post