1.静态导入
2.自动装箱/拆箱
3.for-each循环
4.可变参数
5.枚举
6.泛型
7.注解
使用这些特性有助于编写更加清晰,精悍,安全的代码。
静态导入:
import static java.lang.Math.*; import static java.lang.System.out; //import static java.math.*; public class StaticAttr { public static void main(String[] args) { out.println("xx"); out.println(PI); out.println(abs(-1)); out.println(pow(1.5,2.2)); } }
自动装箱/拆箱
public class Zy2 { public static void main(String[] args) { Integer a=300; Integer b=300; System.out.println(a.equals(b)); System.out.println(a==b); System.out.println("-------------"); Integer d=127; Integer c=127; System.out.println(d.equals(c)); System.out.println(d==c); System.out.println("-------------"); Integer it=Integer.valueOf(103); Integer it2=Integer.valueOf(103); System.out.println(it.equals(it2)); System.out.println(it==it2); } }
for-each循环
public class Foreach { public static void main(String[] args) { ArrayList list=new ArrayList(); list.add(1); list.add(2); list.add(3); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } Set<String> set=new HashSet<>(); set.add("a"); set.add("b"); set.add("c"); for (String s: set) { System.out.println(s); } } }
可变参数
public class Foreach { public static void main(String[] args) { ArrayList list=new ArrayList(); list.add(1); list.add(2); list.add(3); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } Set<String> set=new HashSet<>(); set.add("a"); set.add("b"); set.add("c"); for (String s: set) { System.out.println(s); } } }
枚举
public enum Season implements IPrinter{ 春天{ @Override public void show(){ System.out.println("春天"); } public void print(){ //TO DO } }, 夏天{ @Override public void show(){ System.out.println("夏天"); } public void print(){ //TO DO } }, 秋天{ @Override public void show(){ System.out.println("秋天"); } @Override public void print(){ //TO DO } }, 冬天{ @Override public void show(){ System.out.println("冬天"); } @Override public void print(){ //TO DO } } ; public abstract void show(); }
public interface IPrinter { void print(); }
import java.util.Scanner; public class Zy5 { public static void main(String[] args) { Season Season1= Season.春天; Season Season2= Season.冬天; Season Season3= Season.夏天; Season[] seasons = Season.values(); for (Season xx:seasons) { System.out.println(xx.name()); System.out.println(xx.ordinal()); } Scanner scanner = new Scanner(System.in); System.out.println("请输入一个季节:"); String temp = scanner.next(); //将string--->枚举类型 Season se=Season.valueOf(temp); se.show(); System.out.println("请选择一个季节编号:"); int no = scanner.nextInt(); //将数字-->枚举类型 Season se1 = Season.values()[no]; se1.show(); } }
技术总结:
静态导入语句使用 import static 语句
导入指定类的单个静态属性:
import static java.lang.System.out
导入指定类全部静态属性
import static java.lang.Math.*