------- Android培训 、Java培训、期待与您交流!-------
一.JDK 1.7新特性
1.1 异常处理改进
try-with-resources语句
public class Test {
public static void main(String[] args) {
try(FileInputStream fin=new FileInputStream("res.txt"))//放在这其中的资源必须继承AutoCloseable才可以
{
int x;
while((x=fin.read())!=-1)
{
System.out.print((char)x);
}}
catch (IOException e) {e.printStackTrace();}}}
捕获多个异常
try
{
}
catch(A|B|C ex)
{
}
//只有当A,B,C没有继承关系时才可以,A,B,C默认是常量。
public class Test {
public Test(int x) throws A, B
{
if(x==0)
throw new A("异常A");
else
throw new B("异常B");
}
public static void main(String[] args) {
try {
Test t=new Test(2);
} catch (A |B e) {
e.printStackTrace();
}
}
}
class A extends Exception
{
public A(String str)
{
System.out.println(str);
}
}
class B extends Exception
{
public B(String str)
{
System.out.println(str);
}
}
使反射方法的异常处理简单化
在反射方法的方法中可能抛出多个异常,当然你可以使用捕获多个异常的方法,但是还有一种更简单的方法是JDK1.7为次提供了一个新的父类异常接口:ReflectiveOperationException。
1.2 使用文件的改进
Path
public class Test {
public static void main(String[] args) throws IOException {
String fileSource="in.txt";
String fileTo =null;
String path=System.getProperty("user.dir");
File dir=Paths.get(path).toFile();
File[] file=dir.listFiles();
for(File f:file)
{
if(f.getName().equalsIgnoreCase(fileSource)){
fileTo=fileSource.substring(0, fileSource.lastIndexOf('.'))+"-副本"+(int)(Math.random()*9+1)+fileSource.substring(fileSource.lastIndexOf('.'), fileSource.length());
break;
}
}
Files.copy(Paths.get(fileSource), Paths.get(fileTo));
System.out.println(fileTo);
}
}
public class Test {
public static void main(String[] args) {
Path src=Paths.get(System.getProperty("user.dir"));
Path dst=Paths.get("in.txt");
Path path0=src.resolve(dst);
System.out.println(path0);//C:\Users\Administrator\Desktop\文件\Struct\in.txt
Path path1=dst.resolve(src);
System.out.println(path1);//C:\Users\Administrator\Desktop\文件\Struct
Path path2=src.resolveSibling(dst);
System.out.println(path2);//C:\Users\Administrator\Desktop\文件\Struct
Path path3=dst.resolveSibling(src);
System.out.println(path3);//C:\Users\Administrator\Desktop\文件\Struct
}
}
读取文件
public class Test {
public static void main(String[] args) throws IOException {
Path src=Paths.get("in.txt");
byte[] data=Files.readAllBytes(src);
String str=new String(data);
System.out.println(str);
Path dst=Paths.get("out.txt");
String rpc="1234567890";
Files.write(dst,rpc.getBytes());
}
}
创建文件和目录
public class Test {
public static void main(String[] args) throws IOException {
Path p=Paths.get(System.getProperty("user.dir")+"\\RPC");
Path p1=Paths.get(System.getProperty("user.dir")+"\\RPC\\RGB");
Files.createDirectory(p);//中间目录必须存在
Files.createDirectories(p1);//根目录存在即可
}
}
复制、移动和删除文件
public class Test {
public static void main(String[] args) throws IOException {
Path p=Paths.get("in.txt");
Path p1=Paths.get("out.txt");
Path src=Paths.get(System.getProperty("user.dir"));
Path dst=Paths.get(System.getProperty("user.dir")+"\\RPC");
Files.copy(p, p1,StandardCopyOption.REPLACE_EXISTING);
Files.move(p,dst,StandardCopyOption.REPLACE_EXISTING);
Files.delete(p);
}
}
1.3 实现equals、hashcode、compareTo
安全的NULL值测试
return Objects.equals(first, other.first)&&Objects.equals(last, other.last)
计算哈希码
Objects.hashCode(o);
Objects.hash(values);
比较数值类型对象
Integer.compare(x, y)
Long.compare(x, y)//还有其他基本类型
1.4 基础改建
Switch 语句中允许使用 String 类型
switch(str)
{
case "1" :
case "2":
.
.
.
case "n":
}
int x=0b010101;
int y=0x1234567A;
int z=0111111;
List<String>list=new ArrayList<>();
数值可加下划线
int x=1_2_3;
float x=1_.0;//错误;
int z=1_f_f;/错误;
1.5 其他改动
将字符串转换为数字
int x=Integer.parseInt("+1");//JDK 1.7之前不行
全局Logger
Logger.getGlobal()
Null检查
Objects.requireNonNull(Object obj)
二.JDK1.8新特性
2.1 lambda表达式
条件:函数式接口:最多只能包含一个抽象函数声明。可以用@FunctionalInterface注解来标识是否满足函数式接口,如果满足以函数式接口规范也可不需用注解来标识。
注意:在lambda表达式中出现异常,要么捕获处理,要么将该表达式赋值给能抛出相同异常的函数式接口。
重点:lambda表达式所接受的对象绝对不能是泛型类型,如果你的函数式接口的方法没有声明异常类型,那么你在使用lambda表达式的时候就只能捕获异常并处理,lambda表达式的使用要上下文推断,不能随便使用,否则会出现错误,被lambda表达式捕获的变量到lambda表达式中全部为常量,例如你在lambda表达式定义的上下文声明定义了一个非常量型的值i=0;那么一旦它被lambda表达式捕获之后,在lambda表达式中,它变成常量,不能改变。不能在lambda表达式所定义的上下文定义和lambda表达式中相同的变量。不能重复定义lambda表达式的参数类型
public static void main(String[] args) {
int i=0;
//A a=()->{int i=0;}; 错误,不能在lambda表达式定义上下文重复变量
//A a=()->{i=2;}; 错误,不能在lambda表达式更改捕获变量的值
}
/*
* lambda表达式的匹配规则:
* 1.参数的类型要一致
* 2.返回值类型要一致
* 3.符合兼容规则:自动拆箱装修功能;符合宽化处理,例如:byte->short,如果窄化处理时,需强制转换;存在类的继承关系也可以。
*/
public interface A<T> {
public T RGB(T t) throws Exception;
}
A<String>a=name->{return name;};
System.out.println(a.RGB("1234"));
public interface A {
public <T>T RGB(T t) throws Exception;
}
public class Test {
public static void main(String[] args) throws Exception {
/*
* lamdba 表达式对象只能接收具体类型对象
*/
//A a=name->{return name;}; //会提示:Illegal lambda expression: Method RGB of type A is generic
A a=Test::getString; //这种形式可以
System.out.println(a.RGB(new Object()));
}
@SuppressWarnings("unchecked")
public static <T> T getString(T str)
{
return (T) str.toString().toUpperCase();
}
}
相关知识:
方法引用
对象::实例方法
类::实例方法
类::静态方法
public interface Function {
public void getName(Object o);
}
public class Test {
public static void main(String[] args) {
Function func=System.out::println;
func.getName("123");
}
}
构造器引用
类::new即可表示一个构造器引用
变量作用域
利用lambda表达式创造的相当于方法里面的内部类,所用的参数默认为常量型。
默认方法
可以在接口中用default声明一个方法,该方法可以有方法实体,
接口中的静态方法
接口中同时也也可以有static修饰的方法。
public interface A {
public <T>T RGB(T t) throws Exception;
public default void s()
{
}
public static void r()
{
}
}
2.2 Sream 常识
Stream自己不会存储元素,元素存储在底层集合中
Stream操作符不会改变源对象
Stream操作符可能是延迟执行
Stream 操作流水线:1.创建一个Stream,2.在一个或多个步骤中,指定将初始Stream转换为另一个Stream的中间操作。3.使用一个终止操作来产生一个结果。该操作会强制它之前的的延迟操作立即执行,在这之后,Stream就不会被执行。
2.3 JavaFX
public class FX extends Application {
@Override
public void start(Stage arg0) throws Exception {
Label label=new Label();
label.setText("中华人民共和国");
label.setFont(new Font(100));
label.autosize();
arg0.setScene(new Scene(label));
arg0.setTitle("国家");
arg0.centerOnScreen();
arg0.show();
}
}
public static void main(String[] args) {
FX fx=new FX();
fx.launch(FX.class,"1234");
}
}