java编译器导致的问题
首先,让我们来看一段代码:
package com.test; import java.util.HashMap; import java.util.Map; public class Foo { public static String X = "X"; public static class Bar { public static final String Y = "Y" + X; } private static Map<String, String> map = new HashMap<>(); static { map.put(Foo.X, "1"); map.put(Bar.Y, "2"); } public static String getValue(String key) { return map.get(key); } }
package com.test; public class Test { public static void main(String[] args) { System.out.println(Foo.getValue(Foo.Bar.Y)); } }
运行结果:
null
为什么呢?因为
public static String X = "X";
等同于:
public static String X = null; static { X = "X"; }
Foo被JVM实例化为 Class 对象时,X的值还是null,所以map.put(Bar.Y, "2"); 执行时,key = "Y" + null导致的。
下面我们再来看一下网上的这篇文章:http://blog.csdn.net/psyl/article/details/581742,代码如下:
程序一:
package com.test; class Singleton { private static Singleton obj = new Singleton(); public static int counter1; public static int counter2 = 0; private Singleton() { counter1++; counter2++; } public static Singleton getInstance() { return obj; } }
程序二:
package com.test; public class MyMain { public static void main(String[] args) { Singleton obj = Singleton.getInstance(); System.out.println("obj.counter1==" + obj.counter1); System.out.println("obj.counter2==" + obj.counter2); } }
运行结果:
obj.counter1==1
obj.counter2==0
你有没有被此结果吓一跳?乍看程序代码,你很可能会认为counter1和counter2的值一定会相等,但执行结果显然不是如此。其实,程序1被编译后的程序应该等同于下面的程序三:
package com.test; class Singleton { private static Singleton obj; public static int counter1; public static int counter2; static { // 这就是class constructor // 在进入此class constructor之前,class已经被JVM // 配置好内存,所有的static field都会被先设定为0, // 所以此时counter1和counter2都已经是0,且singleton为null obj = new Singleton(); // 问题皆由此行程序产生 // counter1不会在此被设定为0 counter2 = 0; // counter2再被设定一次0(其实是多此一举) } private Singleton() { // 这是instance constructor counter1++; counter2++; } public static Singleton getInstance() { return obj; } }
这是因为:当class具有static field,且直接在宣告处透过「=...」的方式设定其值时,编译器会自动将这些叙述依序搬到class constructor内。同样地,当class具有instance field,且直接在宣告处透过「=...」的方式设定其值时,编译器会自动将这些叙述依序搬到instance constructor内。
此程序在class constructor内,还未将static field初始化时(这时候,counter1和counter2都是0),就呼叫instance constructor,而instance constructor竟然还会去更动static field的值,使得counter1和counter2都变成1。然后instance constructor执行完,回到class constructor,再把counter2的值设为0(但是counter1维持不变)。最后的结果:counter1等于1,counter2等于0。
欲改正程序一,方法有三:
方法一:将singleton field的宣告调到counter1与counter2 field之后。这是最好的作法。
方法二:将counter2=0的宣告中,「=0」的部分删除。
方法三:将初始化的动作搬到class constructors内,自行撰写,而不依赖编译器产生。这是最保险的作法。
其实,为了避免犯这种错误,可以:
1.熟读Java Language Specification
2.在有疑问时,使用J2SDK所提供的javap来反组译Java Bytecode,直接观察编译后的结果
下面是我用javap来反组译程序一的示范:
C:/>javap -c -classpath . Singleton Compiled from MyMain.java class Singleton extends java.lang.Object { public static int counter1; public static int counter2; public static Singleton getInstance(); static {}; } Method Singleton() 0 aload_0 1 invokespecial #1 <Method java.lang.Object()> 4 getstatic #2 <Field int counter1> 7 iconst_1 8 iadd 9 putstatic #2 <Field int counter1> 12 getstatic #3 <Field int counter2> 15 iconst_1 16 iadd 17 putstatic #3 <Field int counter2> 20 return Method Singleton getInstance() 0 getstatic #4 <Field Singleton obj> 3 areturn Method static {} 0 new #5 <Class Singleton> 3 dup 4 invokespecial #6 <Method Singleton()> 7 putstatic #4 <Field Singleton obj> 10 iconst_0 11 putstatic #3 <Field int counter2> 14 return
其实Java的syntactic sugar并不算多,C#的syntactic sugar才真的是无所不在,也因此C#的初学者更容易犯这种错。许多C#的书都会一边介绍C#语法,一边介绍编译之后MSIL(.NET的中间语言,类似Java的Bytecode)的结果,然而Java的书却鲜少这么做。
最后只想说:只要有心,其实这一类的错误仍是可以避免的。
posted on 2017-10-14 17:31 bijian1013 阅读(187) 评论(0) 收藏 举报
浙公网安备 33010602011771号