JDK1.5新特性:
1.自动装箱与拆箱
自动装箱,只需将该值赋给一个类型包装器引用,java会自动创建一个对象。
自动拆箱,只需将该对象值赋给一个基本类型即可。
java——类的包装器
类型包装器有:Double,Float,Long,Integer,Short,Character和Boolean
2.枚举
把集合里的对象元素一个一个提取出来。枚举类型使代码更具可读性,理解清晰,易于维护。枚举类型是强类型的,从而保证了系统安全性。
而以类的静态字段实现的类似替代模型,不具有枚举的简单性和类型安全性。
简单的用法:JavaEnum简单的用法一般用于代表一组常用常量,可用来代表一类相同类型的常量值。
复杂用法:Java为枚举类型提供了一些内置的方法,同事枚举常量还可以有自己的方法。可以很方便的遍历枚举对象。
3.静态导入
通过使用 import static,就可以不用指定 Constants 类名而直接使用静态成员,包括静态方法。 import xxxx 和 import static xxxx的区别是前者一般导入的是类文件如import java.util.Scanner;
后者一般是导入静态的方法,import static java.lang.System.out。
4.可变参数(Varargs)
/**可变参数本质就是一个数组,arr就是一个数组的引用地址(反编译工具查看源代码) 一个方法 可以有可变参数和普通参数,但是可变参数必须放到参数列表末尾; 一个方法 有且只能有一个可变参数; **/ public int getSum(int value,int … arr){ }
5.内省(Introspector)
是 Java语言对Bean类属性、事件的一种缺省处理方法。例如类A中有属性name,那我们可以通过getName,setName来得到其值或者设置新 的值。
通过getName/setName来访问name属性,这就是默认的规则。Java中提供了一套API用来访问某个属性的getter /setter方法,通过这些API可以使你不需要了解这个规则(但你最好还是要搞清楚),
这些API存放于包java.beans中。一般的做法是通过类Introspector来获取某个对象的BeanInfo信息,然后通过BeanInfo来获取属性的描述器 (PropertyDescriptor),通过这个属性描述器就
可以获取某个属性对应的getter/setter方法,然后我们就可以通过反射机制来 调用这些方法。
6.泛型(Generic)
C++ 通过模板技术可以指定集合的元素类型,而Java在1.5之前一直没有相对应的功能。一个集合可以放任何类型的对象,相应地从集合里面拿对象的时候我们也 不得不对他们进行强制得类型转换。
引入了泛型,它允许指定集合里元素的类型,这样你可以得到强类型在编译时刻进行类型检查的好处。
7.For-Each循环
For-Each循环得加入简化了集合的遍历。假设我们要遍历一个集合对其中的元素进行一些处理。
JDK 1.6新特性
暂无
JDK 1.7 新特性
1. switch中可以使用字串了
String s = "test"; switch (s) { case "test" : System.out.println("test"); case "test1" : System.out.println("test1"); break ; default : System.out.println("break"); break ; }
2. 泛型实例化类型自动推断。
// Pre-JDK 7 List<String> lst1 = new ArrayList<String>(); // JDK 7 supports limited type inference for generic instance creation List<String> lst2 = new ArrayList<>();
3. 自定义自动关闭类
//以下是jdk7 api中的接口,(不过注释太长,删掉了close()方法的一部分注释) /** * A resource that must be closed when it is no longer needed. * * @author Josh Bloch * @since 1.7 */ public interface AutoCloseable { /** * Closes this resource, relinquishing any underlying resources. * This method is invoked automatically on objects managed by the * {@code try}-with-resources statement. * */ void close() throws Exception; } //只要实现该接口,在该类对象销毁时自动调用close方法,你可以在close方法关闭你想关闭的资源,例子如下 class TryClose implements AutoCloseable { @Override public void close() throw Exception { System.out.println(" Custom close method … close resources "); } } //请看jdk自带类BufferedReader如何实现close方法(当然还有很多类似类型的类) public void close() throws IOException { synchronized (lock) { if (in == null) return; in.close(); in = null; cb = null; } }
4. 新增一些取环境信息的工具方法
FileSystem.getJavaIoTempDir() // IO临时文件夹 FileSystem.getJavaHomeDir() // JRE的安装目录 FileSystem.getUserHomeDir() // 当前用户目录 FileSystem.getUserDir() // 启动java进程时所在的目录 .......
5. try(){ } catch(){}资源自动关闭
import java.io.*; // Copy from one file to another file character by character. // Pre-JDK 7 requires you to close the resources using a finally block. public class FileCopyPreJDK7 { public static void main(String[] args) { BufferedReader in = null; BufferedWriter out = null; try { in = new BufferedReader(new FileReader("in.txt")); out = new BufferedWriter(new FileWriter("out.txt")); int charRead; while ((charRead = in.read()) != -1) { System.out.printf("%c ", (char)charRead); out.write(charRead); } } catch (IOException ex) { ex.printStackTrace(); } finally { // always close the streams try { if (in != null) in.close(); if (out != null) out.close(); } catch (IOException ex) { ex.printStackTrace(); } } try { in.read(); // Trigger IOException: Stream closed } catch (IOException ex) { ex.printStackTrace(); } } } //jdk7之后 import java.io.*; // Copy from one file to another file character by character. // JDK 7 has a try-with-resources statement, which ensures that // each resource opened in try() is closed at the end of the statement. public class FileCopyJDK7 { public static void main(String[] args) { try (BufferedReader in = new BufferedReader(new FileReader("in.txt")); BufferedWriter out = new BufferedWriter(new FileWriter("out.txt"))) { int charRead; while ((charRead = in.read()) != -1) { System.out.printf("%c ", (char)charRead); out.write(charRead); } } catch (IOException ex) { ex.printStackTrace(); } } }
6. 在try catch异常扑捉中,一个catch可以写多个异常类型,用"|"隔开
//jdk7 以前 try { ...... } catch(ClassNotFoundException ex) { ex.printStackTrace(); } catch(SQLException ex) { ex.printStackTrace(); } //jdk7例子如下 try { ...... } catch(ClassNotFoundException|SQLException ex) { ex.printStackTrace(); }
JDK8十大特性
1.Lambda表达式
public void testLambda(){ List<Integer> list = Lists.newArrayList(1,2,3,4,5,6,7,8,9,10); list.forEach(System.out::println); list.forEach(e -> System.out.println("方式二:"+e)); }
2.Stream函数式操作流元素集合
public void testStream(){ List<Integer> nums = Lists.newArrayList(1,1,null,2,3,4,null,5,6,7,8,9,10); System.out.println( nums.stream()//转成Stream .filter(team -> team!=null)//过滤 .distinct()//去重 .mapToInt(num->num*2)//map操作 .skip(2)//跳过前2个元素 .limit(4)//限制取前4个元素 .peek(System.out::println)//流式处理对象函数 .sum()//求和 ); }
3.接口新增:默认方法与静态方法
public interface JDK8Interface1 { //1.接口中可以定义静态方法了 public static void staticMethod(){ System.out.println("接口中的静态方法"); } //2.使用default之后就可以定义普通方法的方法体了 public default void defaultMethod(){ System.out.println("接口中的默认方法"); } }
4.方法引用,与Lambda表达式联合使用
public void testMethodReference(){ //构造器引用。语法是Class::new,或者更一般的Class< T >::new,要求构造器方法是没有参数; final Car car = Car.create( Car::new ); final List< Car > cars = Arrays.asList( car ); //静态方法引用。语法是Class::static_method,要求接受一个Class类型的参数; cars.forEach( Car::collide ); //任意对象的方法引用。它的语法是Class::method。无参,所有元素调用; cars.forEach( Car::repair ); //特定对象的方法引用,它的语法是instance::method。有参,在某个对象上调用方法,将列表元素作为参数传入; final Car police = Car.create( Car::new ); cars.forEach( police::follow ); } public static class Car { public static Car create( final Supplier< Car > supplier ) { return supplier.get(); } public static void collide( final Car car ) { System.out.println( "静态方法引用 " + car.toString() ); } public void repair() { System.out.println( "任意对象的方法引用 " + this.toString() ); } public void follow( final Car car ) { System.out.println( "特定对象的方法引用 " + car.toString() ); } }
5.引入重复注解
6.类型注解
7.最新的Date/Time API (JSR 310)
8.新增base64加解密API
public void testBase64(){ final String text = "就是要测试加解密!!abjdkhdkuasu!!@@@@"; String encoded = Base64.getEncoder() .encodeToString( text.getBytes( StandardCharsets.UTF_8 ) ); System.out.println("加密后="+ encoded ); final String decoded = new String( Base64.getDecoder().decode( encoded ), StandardCharsets.UTF_8 ); System.out.println( "解密后="+decoded ); }
9.数组并行(parallel)操作
public void testParallel(){ long[] arrayOfLong = new long [ 20000 ]; //1.给数组随机赋值 Arrays.parallelSetAll( arrayOfLong, index -> ThreadLocalRandom.current().nextInt( 1000000 ) ); //2.打印出前10个元素 Arrays.stream( arrayOfLong ).limit( 10 ).forEach( i -> System.out.print( i + " " ) ); System.out.println(); //3.数组排序 Arrays.parallelSort( arrayOfLong ); //4.打印排序后的前10个元素 Arrays.stream( arrayOfLong ).limit( 10 ).forEach( i -> System.out.print( i + " " ) ); System.out.println(); }
10.JVM的PermGen空间被移除:取代它的是Metaspace(JEP 122)元空间
//-XX:MetaspaceSize初始空间大小,达到该值就会触发垃圾收集进行类型卸载,同时GC会对该值进行调整 //-XX:MaxMetaspaceSize最大空间,默认是没有限制 //-XX:MinMetaspaceFreeRatio在GC之后,最小的Metaspace剩余空间容量的百分比,减少为分配空间所导致的垃圾收集 //-XX:MaxMetaspaceFreeRatio在GC之后,最大的Metaspace剩余空间容量的百分比,减少为释放空间所导致的垃圾收集