java5新特性(简述十大新特性)
1、泛型所谓类型擦除指的就是Java源码中的范型信息只允许停留在编译前期,而编译后的字节码文件中将不再保留任何的范型信息。也就是说,范型信息在编译时将会被全部删除,其中范型类型的类型参数则会被替换为Object类型,并在实际使用时强制转换为指定的目标数据类型。而C++中的模板则会在编译时将模板类型中的类型参数根据所传递的指定数据类型生成相对应的目标代码。 Map<Integer,Integer> squares = new HashMap<Integer,Integer>();
public static <A extends Number> double sum(Box<A> box1,Box<A> box2){ double total = 0; for (Iterator<A> i = box1.contents.iterator(); i.hasNext(); ) { total = total + i.next().doubleValue(); } for (Iterator<A> i = box2.contents.iterator(); i.hasNext(); ) { total = total + i.next().doubleValue(); } return total; } 2、枚举
public void testEnumMap(PrintStream out) throws IOException { // Create a map with the key and a String message EnumMap<AntStatus,String> antMessages = new EnumMap<AntStatus,String>(AntStatus.class); // Initialize the map antMessages.put(AntStatus.INITIALIZING,"Initializing Ant..."); antMessages.put(AntStatus.COMPILING,"Compiling Java classes..."); antMessages.put(AntStatus.COPYING,"Copying files..."); antMessages.put(AntStatus.JARRING,"JARring up files..."); antMessages.put(AntStatus.ZIPPING,"ZIPping up files..."); antMessages.put(AntStatus.DONE,"Build complete."); antMessages.put(AntStatus.ERROR,"Error occurred."); // Iterate and print messages for (AntStatus status : AntStatus.values() ) { out.println("For status " + status + ",message is: " + antMessages.get(status)); } }
public String getDescription() { switch(this) { case ROSEWOOD: return "Rosewood back and sides"; case MAHOGANY: return "Mahogany back and sides"; case ZIRICOTE: return "Ziricote back and sides"; case SPRUCE: return "Sitka Spruce top"; case CEDAR: return "Wester Red Cedar top"; case AB_ROSETTE: return "Abalone rosette"; case AB_TOP_BORDER: return "Abalone top border"; case IL_DIAMONDS: return "Diamonds and squares fretboard inlay"; case IL_DOTS: return "Small dots fretboard inlay"; default: return "Unknown feature"; } } 3、自动拆箱/装箱将primitive类型转换成对应的wrapper类型:Boolean、Byte、Short、Character、Integer、Long、Float、Double 4、可变参数private String print(Object... values) { StringBuilder sb = new StringBuilder(); for (Object o : values) { sb.append(o.toString()) .append(" "); } return sb.toString(); } 5、注解
@Documented @Inherited @Retention(RetentionPolicy.RUNTIME) public @interface InProgress { }
6、增强for循环 for/infor/in循环办不到的事情: 7、静态导入import static java.lang.System.err; import static java.lang.System.out; err.println(msg); 8、print输出格式化System.out.printf("Line %d: %s%n",i++,line); // 不是println 9、并发支持(JUC)
class SimpleThreadExceptionHandler implements Thread.UncaughtExceptionHandler { public void uncaughtException(Thread t,Throwable e) { System.err.printf("%s: %s at line %d of %s%n",t.getName(),e.toString(),e.getStackTrace()[0].getLineNumber(),e.getStackTrace()[0].getFileName()); }
10、Arrays、Queue、线程安全StringBuilder
Arrays.sort(myArray); Arrays.toString(myArray) Arrays.binarySearch(myArray,98) Arrays.deepToString(ticTacToe) Arrays.deepEquals(ticTacToe,ticTacToe3)
Queue q = new LinkedList(); //采用它来实现queue ? 1、泛型所谓类型擦除指的就是Java源码中的范型信息只允许停留在编译前期,而编译后的字节码文件中将不再保留任何的范型信息。也就是说,范型信息在编译时将会被全部删除,其中范型类型的类型参数则会被替换为Object类型,并在实际使用时强制转换为指定的目标数据类型。而C++中的模板则会在编译时将模板类型中的类型参数根据所传递的指定数据类型生成相对应的目标代码。————————————————版权声明:本文为CSDN博主「_YourBatman」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。原文链接:https://blog.csdn.net/f641385712/article/details/81783266 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- Java二进制操作(动力节点Java学院整理)
- 什么是脏数据,缓存中是否可能产生脏数据,如果出现脏数据该
- java – 如何从Hibernate MetadataSources中发现完全限定的
- java – ArrayList查找第一个和最后一个元素
- java – SBT build,在Compile上运行子项目的主类并运行
- JSP JSTL <fmt:formatDate>标签:格式化时间
- java – Hibernate验证器,自定义ResourceBundleLocator和Sp
- java – 使用CAS Spring Security实现SSO
- java – Scala点语法(或缺少)
- 详解Java豆瓣电影爬虫――小爬虫成长记(附源码)