Java 语法糖详解
本文旨在从 Java 编译原理角度,通过字节码及 class 文件,了解 Java 中的语法糖原理及用法,学习语法糖背后的原理。若下文中有任何错误,欢迎大家提出指正。
顺手安利一个 class 文件在线反编译网站。
一、什么是语法糖
语法糖也称糖衣语法,指在计算机语言中添加的某种语法,这种语法对语言的功能并没有影响,但是更方便程序员使用。简而言之,语法糖让程序更加简洁,有更高的可读性。
自JDK7开始,Java语言中的语法糖在逐渐丰富,这种语法对语言的功能并没有影响,因为 Java 代码需要运行在 JVM 中,JVM 是并不支持语法糖的,语法糖在程序编译阶段就会被还原成简单的基础语法结构。
二、Java 中有哪些常见的语法糖
首先回顾一下Java编译,javac命令可以将后缀名为.java的源文件编译成后缀名为.class的可以运行于 JVM 的字节码文件。通过查看 com.sun.tools.javac.main.JavaCompiler 的源码,会发现在 compile() 中有一个步骤就是调用 desugar(),这个方法就是负责解语法糖的实现。
Java 中最常用的语法糖主要有泛型、变长参数、条件编译、自动拆装箱、内部类等。
1、switch 支持 String
从JDK7开始,switch 开始支持 String 字符串类型。在此之前 switch 只支持 int、char等基本类型。对于 int 类型,switch 直接进行数值比较;对于 char 类型则是比较
ASCII 码。所以对于编译器来说, switch 中其实只能使用整型。
switch 对 String 的支持,有如下代码:
public class switchDemoString {
public static void main(String[] args) {
String str = "world";
switch (str) {
case "hello":
System.out.println("hello");
break;
case "world":
System.out.println("world");
break;
default:
break;
}
}
}
对 class 文件进行反编译后:
public class switchDemoString
{
public switchDemoString()
{
}
public static void main(String args[])
{
String str = "world";
String s;
switch((s = str).hashCode())
{
default:
break;
case 99162322:
if(s.equals("hello"))
System.out.println("hello");
break;
case 113318802:
if(s.equals("world"))
System.out.println("world");
break;
}
}
}
由此可以看出,switch 是通过 equals() 和 hashCode()方法来实现对 Stirng 字符串类型的支持。同时也能看出 switch 实际上是对字符串的哈希值进行比较的,然后通过 equals() 方法进行检查,防止哈希可能会发生碰撞。所以性能是不如使用枚举进行 switch 或者使用整数常量。
2、泛型
对于泛型,Java 使用的是Code sharing的机制。也就是说,对于 JVM来说,根本不认识Map<String, String> temp 这样的语法。需要在编译阶段通过类型擦除的方式进行解语法糖。
类型擦除的主要过程如下: (1).将所有的泛型参数用其最左边界(最顶级的父类型)类型替换。 (2).移除所有的类型参数。
例一:
Map<String, String> map = new HashMap<String, String>();
map.put("name", "hollis");
map.put("wechat", "Hollis");
反编译后内容如下:
Map map = new HashMap();
map.put("name", "hollis");
map.put("wechat", "Hollis");
例二:
public static <A extends Comparable<A>> A max(Collection<A> xs) {
Iterator<A> xi = xs.iterator();
A w = xi.next();
while (xi.hasNext()) {
A x = xi.next();
if (w.compareTo(x) < 0)
w = x;
}
return w;
}
反编译后内容如下:
public static Comparable max(Collection xs){
Iterator xi = xs.iterator();
Comparable w = (Comparable)xi.next();
while(xi.hasNext())
{
Comparable x = (Comparable)xi.next();
if(w.compareTo(x) < 0)
w = x;
}
return w;
}
由此可以看出,虚拟机中没有泛型,只有普通类和普通方法,所有泛型类的类型参数在编译时都会被擦除,泛型类并没有自己独有的 Class 类对象。
注意:泛型 + 重载
public static void method(List<String> list) {
System.out.println("invoke method(List<String> list)");
}
public static void method(List<Integer> list) {
System.out.println("invoke method(List<Integer> list)");
}
上面这段代码,有两个重载的函数,因为他们的参数类型不同,一个是List<String>另一个是 List<Integer> ,但是,这段代码是编译通不过的。因为参数List<Integer>和 List<String> 编译之后都被擦除了,变成了一样的原生类型 List,擦除动作导致这两个方法的特征签名变得一模一样。
注意:泛型 + catch
泛型的类型参数不能用在 Java 异常处理的 catch 语句中。因为异常处理是由 JVM 在运行时刻来进行的。由于类型信息被擦除,JVM 是无法区分两个异常类型MyException<String>和MyException<Integer>的
3、自动装箱与拆箱
自动装箱就是 Java 自动将原始类型值转换成对应的对象,比如将 int 的变量转换成 Integer 对象,这个过程叫做装箱,反之将 Integer 对象转换成 int 类型值,这个过程叫做拆箱。因为这里的装箱和拆箱是自动进行的非人为转换,所以就称作为自动装箱和拆箱。
自动装箱:
public static void main(String[] args) {
int i = 10;
Integer n = i;
}
反编译后内容如下:
public static void main(String args[])
{
int i = 10;
Integer n = Integer.valueOf(i);
}
自动拆箱:
public static void main(String[] args) {
Integer i = 10;
int n = i;
}
反编译后内容如下:
public static void main(String args[])
{
Integer i = Integer.valueOf(10);
int n = i.intValue();
}
在装箱的时候自动调用的是Integer的valueOf(int)方法。而在拆箱的时候自动调用的是Integer的intValue方法。
所以,装箱过程是通过调用包装器的 valueOf 方法实现的,而拆箱过程是通过调用包装器的 xxxValue 方法实现的。
注意:对象相等比较
public static void main(String[] args) {
Integer a = 1000;
Integer b = 1000;
Integer c = 100;
Integer d = 100;
System.out.println("a == b is " + (a == b));
System.out.println(("c == d is " + (c == d)));
}
输出结果:
a == b is false
c == d is true
在 JDK5 中,在 Integer 的操作上引入了一个新功能来节省内存和提高性能。整型对象通过使用相同的对象引用实现了缓存和重用。适用于整数值区间-128 至 +127。同时结论只适用于自动装箱。使用构造函数创建对象不适用。
4、可变参数
可变参数是在 JDK5 中引入的一个特性。它允许一个方法把任意数量的值作为参数。
代码:
public static void main(String[] args)
{
print("hello", "world");
}
public static void print(String... strs)
{
for (int i = 0; i < strs.length; i++)
{
System.out.println(strs[i]);
}
}
反编译后内容如下:
public static void main(String args[])
{
print(new String[] {
"hello", "world",
});
}
public static transient void print(String strs[])
{
for(int i = 0; i < strs.length; i++)
System.out.println(strs[i]);
}
由此可以看出,可变参数在被使用的时候,首先会创建一个数组,数组的长度就是调用该方法是传递的实参的个数,然后再把参数值全部放到这个数组当中,然后再把这个数组作为参数传递到被调用的方法中。
5、枚举
JDK5 提供了一种新的类型-Java 的枚举类型,关键字enum。
创建枚举代码如下:
public enum t {
SPRING,SUMMER;
}
反编译后内容如下:
public final class T extends Enum
{
private T(String s, int i)
{
super(s, i);
}
public static T[] values()
{
T at[];
int i;
T at1[];
System.arraycopy(at = ENUM$VALUES, 0, at1 = new T[i = at.length], 0, i);
return at1;
}
public static T valueOf(String s)
{
return (T)Enum.valueOf(demo/T, s);
}
public static final T SPRING;
public static final T SUMMER;
private static final T ENUM$VALUES[];
static
{
SPRING = new T("SPRING", 0);
SUMMER = new T("SUMMER", 1);
ENUM$VALUES = (new T[] {
SPRING, SUMMER
});
}
}
通过反编译可以看到,该类是继承了Enum类,同时被 final 关键字修饰,表明该类不可被继承。
当使用 enum 来定义一个枚举类型的时候,编译器会自动帮我们创建一个 final 类型的类继承Enum 类,所以枚举类型不能被继承。
6、条件
—般情况下,程序中的每一行代码都要参加编译。但有时候出于对程序代码优化的考虑,希望只对其中一部分内容进行编译,此时就需要在程序中加上条件,让编译器只对满足条件的代码进行编译,将不满足条件的代码舍弃,这就是条件编译。
条件代码如下:
public class ConditionalCompilation {
public static void main(String[] args) {
final boolean DEBUG = true;
if(DEBUG) {
System.out.println("Hello, DEBUG!");
}
final boolean ONLINE = false;
if(ONLINE){
System.out.println("Hello, ONLINE!");
}
}
}
反编译后内容如下:
public class ConditionalCompilation
{
public ConditionalCompilation()
{
}
public static void main(String args[])
{
boolean DEBUG = true;
System.out.println("Hello, DEBUG!");
boolean ONLINE = false;
}
}
Java 语法的条件编译,是通过判断条件为常量的 if 语句实现的。其原理也是 Java 语言的语法糖。根据 if 判断条件的真假,编译器直接把分支为 false 的代码块消除。
7、数值字面量
在 java 7 中,数值字面量,不管是整数还是浮点数,都允许在数字之间插入任意多个下划线。这些下划线不会对字面量的数值产生影响,目的就是方便阅读。
例如:
public class Test {
public static void main(String... args) {
int i = 10_000;
System.out.println(i);
}
}
反编译后内容如下
public class Test
{
public static void main(String[] args)
{
int i = 10000;
System.out.println(i);
}
}
反编译后就是把_删除了。也就是说 编译器并不认识在数字字面量中的_,需要在编译阶段把他去掉。
8、for-each
对于 for-each,日常开发中必不可少,对比 for 循环要少写很多代码。
例如:
public static void main(String... args) {
String[] strs = {"hello", "world", "smile"};
for (String s : strs) {
System.out.println(s);
}
List<String> strList = ImmutableList.of("hello", "world", "smile");
for (String s : strList) {
System.out.println(s);
}
}
反编译后内容如下:
public static transient void main(String args[])
{
String strs[] = {
"hello", "world", "smile"
};
String args1[] = strs;
int i = args1.length;
for(int j = 0; j < i; j++)
{
String s = args1[j];
System.out.println(s);
}
List strList = ImmutableList.of("hello", "world", "smile");
String s;
for(Iterator iterator = strList.iterator(); iterator.hasNext(); System.out.println(s))
s = (String)iterator.next();
}
可以看出,for-each 实现原理其实就是使用了普通的 for 循环和迭代器。
注意:List.remove() 方法的使用
for (Student stu : students) {
if (stu.getId() == 2)
students.remove(stu);
}
Iterator 是工作在一个独立的线程中,并且拥有一个 mutex 锁。 Iterator 被创建之后会建立一个指向原来对象的单链索引表,当原来的对象数量发生变化时,这个索引表的内容不会同步改变,所以当索引指针往后移动的时候就找不到要迭代的对象,所以按照 fail-fast 原则 Iterator 会马上抛出 java.util.ConcurrentModificationException 异常。
9、try-with-resource
Java 里,对于文件操作 IO 流、数据库连接等开销非常昂贵的资源,用完之后必须及时通过 close 方法将其关闭,否则资源会一直处于打开状态,可能会导致内存泄露等问题。
关闭资源的常用方式就是在finally块里是释放,即调用close方法。
常规写法:
public static void main(String[] args) {
BufferedReader br = null;
try {
String line;
br = new BufferedReader(new FileReader("/Users/smile-jt/Downloads/test.xml"));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
// handle exception
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException ex) {
// handle exception
}
}
}
Java 7 开始,JDK 提供了一种更好的方式关闭资源,使用try-with-resources语句,改写一下上面的代码:
public static void main(String... args) {
try (BufferedReader br = new BufferedReader(new FileReader("/Users/smile-jt/Downloads/test.xml"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
// handle exception
}
}
反编译后内容如下:
public static transient void main(String args[])
{
BufferedReader br;
Throwable throwable;
br = new BufferedReader(new FileReader("/Users/smile-jt/Downloads/test.xml"));
throwable = null;
String line;
try
{
while((line = br.readLine()) != null)
System.out.println(line);
}
catch(Throwable throwable2)
{
throwable = throwable2;
throw throwable2;
}
if(br != null)
if(throwable != null)
try
{
br.close();
}
catch(Throwable throwable1)
{
throwable.addSuppressed(throwable1);
}
else
br.close();
break MISSING_BLOCK_LABEL_113;
Exception exception;
exception;
if(br != null)
if(throwable != null)
try
{
br.close();
}
catch(Throwable throwable3)
{
throwable.addSuppressed(throwable3);
}
else
br.close();
throw exception;
IOException ioexception;
ioexception;
}
}
从常规写法到 JDK7 的简化写法,看上去这种新的语法糖看上去好像优雅很多。但是通过反编译可以看出,那些我们没有做的关闭资源的操作,编译器都帮我们做了。
10、Lambda 表达式
关于 lambda 表达式,也是一个语法糖。实现方式主要是依赖了几个 JVM 底层提供的 lambda 相关 api。
List.lambda 表达式(简单):
public static void main(String... args) {
List<String> strList = ImmutableList.of("hello", "world", "smile");
strList.forEach( s -> { System.out.println(s); } );
}
反编译后代码如下:
public static /* varargs */ void main(String ... args) {
ImmutableList strList = ImmutableList.of((Object)"hello", (Object)"world", (Object)"smile");
strList.forEach((Consumer<String>)LambdaMetafactory.metafactory(null, null, null, (Ljava/lang/Object;)V, lambda$main$0(java.lang.String ), (Ljava/lang/String;)V)());
}
private static /* synthetic */ void lambda$main$0(String s) {
System.out.println(s);
}
可以看到,在forEach方法中,其实是调用了java.lang.invoke.LambdaMetafactory#metafactory方法,该方法的第四个参数 implMethod 指定了方法实现。可以看到这里其实是调用了一个lambda$main$0方法进行了输出。
List.lambda 表达式(复杂):
public static void main(String... args) {
List<String> strList = ImmutableList.of("hello", "world", "smile");
List HollisList = strList.stream().filter(string -> string.contains("Hollis")).collect(Collectors.toList());
HollisList.forEach( s -> { System.out.println(s); } );
}
反编译后代码如下:
public static /* varargs */ void main(String ... args) {
ImmutableList strList = ImmutableList.of((Object)"hello", (Object)"world", (Object)"smile");
List<Object> HollisList = strList.stream().filter((Predicate<String>)LambdaMetafactory.metafactory(null, null, null, (Ljava/lang/Object;)Z, lambda$main$0(java.lang.String ), (Ljava/lang/String;)Z)()).collect(Collectors.toList());
HollisList.forEach((Consumer<Object>)LambdaMetafactory.metafactory(null, null, null, (Ljava/lang/Object;)V, lambda$main$1(java.lang.Object ), (Ljava/lang/Object;)V)());
}
private static /* synthetic */ void lambda$main$1(Object s) {
System.out.println(s);
}
private static /* synthetic */ boolean lambda$main$0(String string) {
return string.contains("Hollis");
}
两个 lambda 表达式分别调用了lambda$main$1和lambda$main$0两个方法。
所以,lambda 表达式的实现是依赖了一些底层的 api,在编译阶段,编译器会把 lambda 表达式进行解糖,转换成调用内部 api 的方式。
三、总结
前面介绍了 10 种 Java 中常用的语法糖。所谓语法糖就是提供给开发人员便于开发的一种语法而已。但是这种语法只有开发人员认识。要想被执行,需要进行解糖,即转成 JVM 认识的语法。当我们把语法糖解糖之后,你就会发现其实我们日常使用的这些方便的语法,其实都是一些其他更简单的语法构成的。
有了这些语法糖,我们在日常开发的时候可以大大提升效率,但是同时也要避免过度使用。使用之前最好了解下原理,避免掉坑。