Java语法糖

Java语法糖

自动拆箱和装箱

拆箱就是自动把包装类型转换成基本数据类型 装箱就是自动把基本数据类型转换成包装类型 Java源代码

    public static void main(String... args) throws Exception {
        Integer a=1;
        Integer a1=1;
        Integer b=2;
        Integer c=3;
        int d=c;
        Integer e=321;
        Integer f=321;
        Long g=3L;
        System.out.println(c==d);
        System.out.println(a==a1);
        System.out.println(e==f);
        System.out.println(c==(a+b));
        System.out.println(c.equals(a+b));
        System.out.println(g==(a+b));
        System.out.println(g.equals(a+b));

    }

反编译以后代码

    public static void main(String... args) throws Exception {
        Integer a = Integer.valueOf(1);
        Integer a1 = Integer.valueOf(1);
        Integer b = Integer.valueOf(2);
        Integer c = Integer.valueOf(3);
        int d = c.intValue();
        Integer e = Integer.valueOf(321);
        Integer f = Integer.valueOf(321);
        Long g = Long.valueOf(3L);
        System.out.println(c.intValue() == d);
        System.out.println(a == a1);
        System.out.println(e == f);
        System.out.println(c.intValue() == a.intValue() + b.intValue());
        System.out.println(c.equals(Integer.valueOf(a.intValue() + b.intValue())));
        System.out.println(g.longValue() == (long)(a.intValue() + b.intValue()));
        System.out.println(g.equals(Integer.valueOf(a.intValue() + b.intValue())));
    }

根据上面的代码可以看到

  1. 把基本数据类型赋值给包装类型时会调用Integer.valueOf(int)方法进行自动装箱
  2. 把包装类型赋值给基本类型时,会调用c.intValue()方法进行自动拆箱
  3. 包装类型和基本数据类型用==比较时会自动将包装类型拆箱成基本数据类型
  4. 包装类型和基本数据类型用equals方法比较时会自动将基本类型装箱成包装类型 这些操作都是在编译的时候由编译器自动完成的。 Integer类型-128-127范围的的实例用==比较都是相等的

泛型和类型擦除

在Java语言处于还没有出现泛型的版本时,只能通过Object是所有类型的父类和类型强制转换两个特点的配合来实 现类型泛化。 Java语言中的泛型则不一样,它只在程序源码中存在,在编译后的字节码文件中,就已经替换为原来的原生类型了,并且在相应的地方插入了强制转型代码,因此,对于运行期的Java语言来说,ArrayList<Integer>ArrayList<String>就是同一个类,所以泛型技术实际上是Java语言的一颗语法糖,Java语言中的泛型实现方法称为类型擦除 源代码

    public static void main(String... args) throws Exception {
        Map<String,String> map=new HashMap<String,String>();
        map.put("hello","你好");
        map.put("how are you?","吃了没?");
        System.out.println(map.get("hello"));
        System.out.println(map.get("how are you?"));
    }

反编译代码

  public static void main(String[] args) throws Exception {
    Map map = new HashMap();
    map.put("hello", "你好");
    map.put("how are you?", "吃了没?");
    System.out.println((String)map.get("hello"));
    System.out.println((String)map.get("how are you?"));
  }

遍历循环

源代码

    public static void main(String... args) throws Exception {
        List<String> list=Arrays.asList("hello","world");
        for(String s : list){
            System.out.println(s);
        }
    }

反编译代码

    public static void main(String... args) throws Exception {
        List<String> list = Arrays.asList(new String[]{"hello", "world"});
        Iterator i$ = list.iterator();
        while(i$.hasNext()) {
            String s = (String)i$.next();
            System.out.println(s);
        }

    }

使用遍历循环必须实现Iterable接口,编译器会自动调用Iterable接口去实现遍历

变长参数

源代码

    public static void main(String... args) throws Exception {
       
    }

反编译代码

  public static void main(String[] args)
    throws Exception
  {
    
  }

变长参数在编译的时候,会被编译器转成数组类型

try语句中定义和关闭资源

源代码

    public static void main(String... args) throws Exception {
        try(FileInputStream inputStream=new FileInputStream("123.txx")){
            System.out.println("hello world");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

反编译代码

    public static void main(String... args) throws Exception {
        try {
            FileInputStream inputStream = new FileInputStream("123.txx");
            Throwable var2 = null;
            try {
                System.out.println("hello world");
            } catch (Throwable var12) {
                var2 = var12;
                throw var12;
            } finally {
                if(inputStream != null) {
                    if(var2 != null) {
                        try {
                            inputStream.close();
                        } catch (Throwable var11) {
                            var2.addSuppressed(var11);
                        }
                    } else {
                        inputStream.close();
                    }
                }
            }
        } catch (Exception var14) {
            var14.printStackTrace();
        }

    }

内部类

对枚举和字符串的switch支持

条件编译

枚举类

断言语句

转载于:https://my.oschina.net/chenbkit/blog/2966959

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值