1.静态导入

import static

静态导入前写法:

 

public class TestStatic {  
    public static void main(String[] args) {  
        System.out.println(Integer.MAX_VALUE);  
        System.out.println(Integer.toHexString(42));  
    }  
} 

静态导入后写法

 

 

import static java.lang.System.out;  
import static java.lang.Integer.*;  
  
public class TestStaticImport {  
    public static void main(String[] args) {  
        out.println(MAX_VALUE);  
        out.println(toHexString(42));  
    } 
} 

 

 

2.可变参数

语法: ...

例如: String ... params

特点:可写多个参数

要求:只能为同一类型参数,并且只能写在方法参数的最后一个

 

public static int add(int x,int ... args){
    int sum = x;
    for(int i = 0;i < args.length; i++){
       sum += args[i];
    }
    return sum;
}


3.增强for循环

 

语法:for(type 变量名 : 集合变量名 ){ ... }

注意事项:

迭代变量名 必须在()内定义

集合变量必须是数组或实现了Iterable接口的集合类

例如:

 

public static int add(int x,int ... args){
    int sum = x;
    for(int arg : args){
        sum += arg;
    }
    return sum;
}

 

 

 

相关文章:

  • 2021-12-01
  • 2021-09-05
  • 2021-05-27
  • 2022-12-23
  • 2021-05-20
  • 2021-11-07
猜你喜欢
  • 2022-01-11
  • 2021-08-25
  • 2021-12-13
  • 2022-12-23
  • 2021-10-25
相关资源
相似解决方案