Syntactic sugar in Java

1. Generics and type erasure

        The original generics are in the C# language, since JDK1.5, java has introduced generics, but they are still different;

        Generics in C#, List<int> and List<String> are two different types, they are generated at runtime, this implementation becomes type inflation, which is a true generic; but in java, List<int > and List<String> belong to the same type of List. After compilation, the type will be erased, and the parameter usage will be type-cast;

Source code:

/**
* 在源代码中存在泛型
*/
public static void main(String[] args) {
    Map<String,String> map = new HashMap<String,String>();
    map.put("hello","你好");
    String hello = map.get("hello");
    System.out.println(hello);
}

Compiled code:

public static void main(String[] args) {
    HashMap map = new HashMap(); //类型擦除
    map.put("hello", "你好");
    String hello = (String)map.get("hello");//强制转换
    System.out.println(hello);
}

Why is a generic type actually a type?

These two methods fail to overload and have the same signature after type elimination;

2. Automatic packing and unpacking, variable length parameters

1. Automatic packing and unpacking

      The idea of ​​java is object-oriented programming, which is also where it has always been complacent, but the point that has been criticized is the basic type in java, which also causes a lot of inconvenience in practical applications, so the packaging class is generated; and the basic type Automatic unboxing is available between and packaging types, which is a syntactic sugar;

Source code:

public static void main(String[] args) {
    Integer a = 1;
    int b = 2;
    int c = a + b;
    System.out.println(c);
}

After compilation:

public static void main(String[] args) {
    Integer a = Integer.valueOf(1); // 自动装箱
    byte b = 2;
    int c = a.intValue() + b;//自动拆箱
    System.out.println(c);
}

2. Variable length parameters

Source code:

public class Varargs {
    public static void print(String... args) {
        for(String str : args){
            System.out.println(str);
        }
    }

    public static void main(String[] args) {
        print("hello", "world");
    }
}

After compilation:

public class Varargs {
    public Varargs() {
    }

    public static void print(String... args) {
        String[] var1 = args;
        int var2 = args.length;
        //增强for循环的数组实现方式
        for(int var3 = 0; var3 < var2; ++var3) {
            String str = var1[var3];
            System.out.println(str);
        }

    }

    public static void main(String[] args) {
        //变长参数转换为数组
        print(new String[]{"hello", "world"});
    }
}

3. Enhance the for loop

     Compared with ordinary for loops, it is more concise and easy to understand;

Source code:

public static void main(String[] args) {
    String[] params = new String[]{"hello","world"};
    //增强for循环对象为数组
    for(String str : params){
        System.out.println(str);
    }

    List<String> lists = Arrays.asList("hello","world");
    //增强for循环对象实现Iterable接口
    for(String str : lists){
        System.out.println(str);
    }
}

After compilation:

public static void main(String[] args) {
   String[] params = new String[]{"hello", "world"};
   String[] lists = params;
   int var3 = params.length;
   //数组形式的增强for退化为普通for
   for(int str = 0; str < var3; ++str) {
       String str1 = lists[str];
       System.out.println(str1);
   }

   List var6 = Arrays.asList(new String[]{"hello", "world"});
   Iterator var7 = var6.iterator();
   //实现Iterable接口的增强for使用iterator接口进行遍历
   while(var7.hasNext()) {
       String var8 = (String)var7.next();
       System.out.println(var8);
   }

}

4. Inner classes and enumerations

1. Inner class

       The inner class is syntactic sugar because it is only a compile-time concept. Once compiled, the compiler will generate a separate class file for the inner class named outer$innter.class.

Source code:

public class Outer {
    class Inner{
    }
}

After compilation:

class Outer$Inner {
    Outer$Inner(Outer var1) {
        this.this$0 = var1;
    }
}

2. Enumeration

       The definition of enum classes uses enum. In Java's bytecode structure, there is no enumeration type. The enumeration type is just a syntactic sugar, which is compiled into an ordinary class after compilation. This class inherits java.lang.Enum and is modified by the final keyword.

Source code:

public enum Fruit {
    APPLE,ORINGE
}

After compilation:

public final class Fruit extends Enum
{

    public static Fruit[] values()
    {
        return (Fruit[])$VALUES.clone();
    }

    public static Fruit valueOf(String s)
    {
        return (Fruit)Enum.valueOf(Fruit, s);
    }

    private Fruit(String s, int i)
    {
        super(s, i);
    }
    //枚举类型常量
    public static final Fruit APPLE;
    public static final Fruit ORANGE;
    private static final Fruit $VALUES[];//使用数组进行维护

    static
    {
        APPLE = new Fruit("APPLE", 0);
        ORANGE = new Fruit("ORANGE", 1);
        $VALUES = (new Fruit[] {
            APPLE, ORANGE
        });
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325441874&siteId=291194637