switch constant issue

Jeff Sturm jsturm@one-point.com
Mon Jun 9 17:51:00 GMT 2003


On Mon, 9 Jun 2003, Andrew Haley wrote:
>  > http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/java/class.c.diff?r1=1.150&r2=1.151&f=h
>
> I guess this is OK, but I'm not sure.  It does rather break
> -fno-assume-compiled in that the constant uses early binding, so we
> might up with a different value for a constant that the one in its
> definition.

Good point.  However, javac does an equivalent optimization:

    [jsturm@triton tmp]$ cat Constants.java
    public class Constants {
      public static final int DEBUG = 1;
    }

    [jsturm@triton tmp]$ cat Main.java
    public class Main {
      public static void main(String[] args) {
        System.out.println(Constants.DEBUG);
      }
    }

    [jsturm@triton tmp]$ javac Main.java
    [jsturm@triton tmp]$ javap -c Main
    ...
    Method void main(java.lang.String[])
       0 getstatic #2 <Field java.io.PrintStream out>
-->    3 iconst_1
       4 invokevirtual #3 <Method void println(int)>
       7 return

My guess is they wanted to support pseudo-conditional compilation with
this common idiom:

    public static final boolean DEBUG = false;
    ...
    if (DEBUG) {
         ...
    }

Jeff



More information about the Java mailing list