你知道Java的这些骚操作吗?

目录一、try with catch二、instance of三、不定项参数 …四、跳出多层循环的label五、方法引用总结

一、try with catch

还记得这样的代码吗?我们需要手动的关闭资源的流,不然会造成资源泄漏,因为虚拟机无法管理系统资源的关闭,必须手动释放。

public void manualClose(String fileName) { BufferedReader reader = null; try {   String line;   reader = new BufferedReader(new FileReader(fileName));   while ((line = reader.readLine()) != null) {     ...   } } catch (Exception e) {   ... } finally {   if (reader != null) {     try {       reader.close();     } catch (IOException e) {       ...     }   } }}

骚操作解救你:

public void autoClose(String fileName) { try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {   String line;   while ((line = reader.readLine()) != null) {     ...   } } catch (Exception e) {   ... }}

可以看到,try-with-resources的比前一种方法明显节省了很多代码,资源在try后边的()中生成,在try结束后程序会自动关闭资源。

如果需要声明多个资源,可以在try后面的()中,以;分隔;也就是说,try后边的()可以添加多行语句, 我上篇文章有展示:你肯定能看懂的Java IO相关知识总结

二、instance of

对象是否是这个特定类或者是它的子类的一个实例,返回一个布尔值。左边是对象,右边是类;当对象是右边类或子类所创建对象时,返回true;否则,返回false。

用法:result=objectinstanceofclass参数:Result:布尔类型。Object:必选项。任意对象表达式。Class:必选项。任意已定义的对象类。  publicinterfaceMonster{ } publicstaticclassDinosaurimplementsMonster{ } publicstaticvoidmain(String[]args) {   Dinosaurdinosaur=newDinosaur();   System.out.println(dinosaurinstanceofMonster); }

三、不定项参数 …

格式如下:

参数个数可以0或者多个

public void method(int...args);

业务场景:

1、在业务开发的时候经常之前写一个方法,但是后来业务变动了,需要增加参数,这个时候可以使用这种方式,多传参数,调用的地方不需要覆盖

2、如果一个方法的的不确定参数个数的情况,通常来说我们会重载,但是如果多了很麻烦,这个时候…可以出场了

//方法重载,解决参数个数不确定问题 public void method(){}; public void method(int i){}; public void method(int i, int j){}; public void method(int i, int j, int k){}; 优化之后的形式: public void method(int i,int ...args);

调用的三种方式

public void call(){   //1、 不使用变参   method(1);   //2、 直接调用   method(1,2,23,4,5,6);   //3、 数组调用   int[] arr = {1,2,23,4,5,6};   method(5,arr); }

四、跳出多层循环的label

Java 中的标签是为循环设计的,是为了在多重循环中方便的使用 break 和coutinue ,当在循环中使用 break 或 continue 循环时跳到指定的标签处

publicstaticvoidmain(String[]args) {   for(inti=0;i<5;i++) {     labelA:for(intj=0;j<5;j++) {       for(intk=0;k<5;k++) {         if(k==1) {           breaklabelA;         }         System.out.println(1);       }     }   } }

不推荐这种用法,虽然很骚,但是老老实实的一层一层break 比较好,你觉得呐?

五、方法引用

用Lambda表达式仅仅是调用一些已经存在的方法,除了调用动作外,没有其他任何多余的动作

packageorg.pdool; importjava.util.ArrayList;importjava.util.List; /*** 方法引用测试类* @author 香菜*/publicclassTrytest{ staticList<Player>playerList=newArrayList<>();  // 静态方法 publicstaticvoidprint(Strings) {   System.out.println(s); }  staticclassPlayer{   privateStringname;   publicPlayer(Stringname) {     this.name=name;     playerList.add(this);   }    privatevoidprintName() {     System.out.println(name);   } }  publicstaticvoidmain(String[]args) {   List<String>strList=newArrayList<>();   strList.add("香菜");   strList.add("follow me");   // 1、静态方法引用   strList.forEach(Trytest::print);   // /2、对象方法引用   strList.forEach(System.out::println);   // 3、构造函数   strList.forEach(Player::new);   // 4、对象方法   playerList.forEach(Player::printName); }}

总结

1、在try结束后程序会自动关闭资源

2、instance of 必须是子类

3、参数个数可以0或者多个,重构代码利器

4、使用 break 或 continue 循环时跳到指定的标签处

5、方法调用是除了调用动作外,没有其他任何多余的动作

到此这篇关于你知道Java的这些骚操作吗?的文章就介绍到这了,更多相关Java骚操作内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

美不美乡中水,亲不亲故乡人。

你知道Java的这些骚操作吗?

相关文章:

你感兴趣的文章:

标签云: