[665] 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 (Tientien Li)
Wed Jul 12 16:06:40 1995

Date: Wed, 12 Jul 1995 10:49:14 -0700
From: li@deming.Jpl.Nasa.Gov (Tientien Li)
To: dan.evans@peri.com
Cc: li@deming.Jpl.Nasa.Gov, java-interest@java.sun.com


> 
> > 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.

--
Tientien Li
li@deming.jpl.nasa.gov
-
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