1.静态导入方法

  1. package com.java.new_features_jdk5;
  2. /**
  3. *
  4. * 一般我们导入一个类都用 import com.....ClassName;而静态导入是这样:import static com.....ClassName.*;
  5. * 这里的多了个static,还有就是类名ClassName后面多了个 .* ,意思是导入这个类里的静态方法。当然,也可以只导入某个静态方法,只要把 .* 换成静态方法名就行了。
  6. * 然后在这个类中,就可以直接用方法名调用静态方法,而不必用ClassName.方法名 的方式来调用。
  7. * 这种方法的好处就是可以简化一些操作,例如打印操作System.out.println(...);就可以将其写入一个静态方法print(...),在使用时直接print(...)就可以了。
  8. * 但是这种方法建议在有很多重复调用的时候使用,如果仅有一到两次调用,不如直接写来的方便。
  9. * @author yuahan
  10. *
  11. */
  12. public class _Static_Import {
  13. public static void main(String[] args) {
  14. }
  15. }

2.新增加的for循环

  1. package com.java.new_features_jdk5;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. /**
  5. * 增强的for循环,可以使用在数组和容器中
  6. * @author yuahan
  7. *
  8. */
  9. public class _For {
  10. @SuppressWarnings("serial")
  11. public static void main(String[] args) {
  12. int[] array = {1,2,3,4,5,6,7,8,9,10};
  13. for(int num : array){
  14. System.out.print(num + " ");
  15. }
  16. System.out.println();
  17. List<Integer> list = new ArrayList<Integer>(){{
  18. this.add(1);
  19. this.add(2);
  20. this.add(3);
  21. this.add(4);
  22. this.add(5);
  23. this.add(6);
  24. this.add(7);
  25. this.add(8);
  26. this.add(9);
  27. this.add(10);
  28. }};
  29. for(int num : list){
  30. System.out.print(num + " ");
  31. }
  32. }
  33. }

3.枚举 Enum

  1. package com.java.new_features_jdk5;
  2. /**
  3. *
  4. * 你可以将枚举类型视为特殊的类,因此几乎可以像创建普通类那样创建枚举。
  5. * 枚举类型有附加的特性,有EnumMap和EnumSet两个类。实例化方法中都需要传入枚举类型的类类型,如:
  6. *  EnumSet<_Enum> set = EnumSet.noneOf(_Enum.class);
  7. EnumMap<_Enum,String> map = new EnumMap<_Enum,String>(_Enum.class);
  8. 枚举可以有自己的构造方法,不过构造方法只能私有,这样外部是不能构造出新的枚举中的实例,而只是调用其中的实例。
  9. * @author yuahan
  10. *
  11. */
  12. public enum _Enum {
  13. Better(90),
  14. Good(80),
  15. Ok(70),
  16. Bad(60),
  17. Worse;
  18. private int value;
  19. private _Enum() {
  20. this.value = 30;
  21. }
  22. private _Enum(int value) {
  23. this.value = value;
  24. }
  25. public int getValue() {
  26. return value;
  27. }
  28. public void setValue(int value) {
  29. this.value = value;
  30. }
  31. public static _Enum[] getEnumValues(){
  32. return _Enum.values();
  33. }
  34. public static void _ValuesOf(){
  35. //      _Enum test = _Enum.valueOf("test");//error
  36. //      System.out.println(test);
  37. _Enum Better = _Enum.valueOf("Better");
  38. System.out.println(Better);
  39. }
  40. public static void main(String[] args) {
  41. for(_Enum mark : _Enum.getEnumValues()){
  42. switch(mark){
  43. case Better:
  44. System.out.println(_Enum.Better);
  45. break;
  46. case Good:
  47. System.out.println(_Enum.Good);
  48. break;
  49. case Ok:
  50. System.out.println(_Enum.Ok);
  51. break;
  52. case Bad:
  53. System.out.println(_Enum.Bad);
  54. break;
  55. case Worse:
  56. System.out.println(_Enum.Worse);
  57. break;
  58. }
  59. }
  60. _Enum._ValuesOf();
  61. System.out.println(_Enum.Better.getValue());
  62. }
  63. }

4.反射 Reflect

  1. package com.java.new_features_jdk5;
  2. import java.lang.reflect.Array;
  3. import java.lang.reflect.Constructor;
  4. import java.lang.reflect.Field;
  5. import java.lang.reflect.Method;
  6. import java.lang.reflect.Modifier;
  7. import java.util.Arrays;
  8. /**
  9. * Reflection 是 Java 程序开发语言的特征之一,它允许运行中的 Java 程序对自身进行检查,或者说“自审”,并能直接操作程序的内部属性。
  10. * Java 的这一能力在实际应用中也许用得不是很多,但是在其它的程序设计语言中根本就不存在这一特性
  11. * JavaDoc 中对每个类和其中的方法有非常详细介绍。主要有:
  12. * Class   ------- java.lang
  13. * Package ------- java.lang
  14. *
  15. * Array
  16. * Field
  17. * Method
  18. * Modifier
  19. * Constructor
  20. * @author yuahan
  21. *
  22. */
  23. public class _Reflect {
  24. private int id;
  25. private String name;
  26. private String[] hobbies;
  27. public _Reflect(){}
  28. public _Reflect(int id){
  29. this.id = id;
  30. }
  31. public _Reflect(int id, String name, String[] hobbies) {
  32. super();
  33. this.id = id;
  34. this.name = name;
  35. this.hobbies = hobbies;
  36. }
  37. public int getId() {
  38. return id;
  39. }
  40. public void setId(int id) {
  41. this.id = id;
  42. }
  43. public String getName() {
  44. return name;
  45. }
  46. public void setName(String name) {
  47. this.name = name;
  48. }
  49. public String[] getHobbies() {
  50. return hobbies;
  51. }
  52. public void setHobbies(String[] hobbies) {
  53. this.hobbies = hobbies;
  54. }
  55. public static void main(String[] args) throws Exception{
  56. //---------------------------------basic---------------------------------
  57. System.out.println(_Reflect.class.getSimpleName());
  58. System.out.println(_Reflect.class.getName());
  59. System.out.println(_Reflect.class.getPackage());
  60. System.out.println(_Reflect.class.getSuperclass().getName());
  61. System.out.println(int[].class.getName());
  62. System.out.println(_Reflect[].class.getName());
  63. //      //--------------------------------- Method ---------------------------------
  64. Method[] methods = _Reflect.class.getMethods();
  65. for(Method method : methods){
  66. System.out.println(method + ": " +method.getDeclaringClass().getName());
  67. }
  68. //      //--------------------------------- isXXX ---------------------------------
  69. System.out.println(Comparable.class.isInterface());
  70. System.out.println(int.class.isPrimitive());
  71. System.out.println(int[].class.isArray());
  72. //      //--------------------------------- Modifier 修饰符 ---------------------------------
  73. System.out.println(Modifier.isPublic(_Reflect.class.getModifiers()));
  74. Class<?>[] classes = null;
  75. System.out.println(Modifier.isPublic(_Reflect.class.getMethod("getId",classes).getModifiers()));
  76. //isAssignableFrom    isInstance
  77. System.out.println(Number.class.isAssignableFrom(Integer.class));
  78. System.out.println(Number.class.isInstance(1));
  79. //---------------------------------Field---------------------------------
  80. _Reflect _Reflect = new _Reflect();
  81. System.out.println(_Reflect.getId());
  82. System.out.println(_Reflect.getName());
  83. System.out.println(Arrays.toString(_Reflect.getHobbies()));
  84. Field[] fields = _Reflect.class.getDeclaredFields();
  85. for(Field field : fields){
  86. if(field.getType() == int.class){
  87. field.setAccessible(true);
  88. field.setInt(_Reflect, 1);
  89. }else if(field.getType() == String.class){
  90. field.setAccessible(true);
  91. field.set(_Reflect, "1");
  92. }else if(field.getType() == String[].class){
  93. field.setAccessible(true);
  94. field.set(_Reflect, new String[]{"1","1"});
  95. }
  96. }
  97. System.out.println(_Reflect.getId());
  98. System.out.println(_Reflect.getName());
  99. System.out.println(Arrays.toString(_Reflect.getHobbies()));
  100. //      //---------------------------------new instance---------------------------------
  101. Constructor<_Reflect> constructor = _Reflect.class.getConstructor(new Class[]{int.class,String.class,String[].class});
  102. _Reflect _reflect = constructor.newInstance(new Object[]{1,"1",new String[]{"1","1"}});
  103. System.out.println(_reflect.getId());
  104. System.out.println(_reflect.getName());
  105. System.out.println(Arrays.toString(_Reflect.getHobbies()));
  106. Class<?> clazz = Class.forName("com.java.new_features_jdk5._Reflect");
  107. _Reflect clazzes = (_Reflect)clazz.newInstance();
  108. System.out.println(clazzes.getId());
  109. System.out.println(clazzes.getName());
  110. System.out.println(Arrays.toString(clazzes.getHobbies()));
  111. //---------------------------------Array---------------------------------
  112. //---------------------------------0---------------------------------
  113. int[] ints0 = (int[])Array.newInstance(int.class, 3);
  114. Array.setInt(ints0, 0, 0);
  115. Array.setInt(ints0, 1, 1);
  116. Array.setInt(ints0, 2, 2);
  117. //    //Array.setInt(ints, 3, 3); //java.lang.ArrayIndexOutOfBoundsException
  118. System.out.println(Arrays.toString(ints0));
  119. //---------------------------------1---------------------------------
  120. int[][][] ints3 = (int[][][])Array.newInstance(int.class,2,3,4);
  121. System.out.println(ints3.length);
  122. System.out.println(ints3[0].length);
  123. System.out.println(ints3[0][0].length);
  124. int[][] ints3_1_row0_content = new int[][]{{1,2,3,4},{1,2,3,4},{1,2,3,4}};
  125. int[][] ints3_1_row1_content = new int[][]{{11,22,33,44},{11,22,33,44},{11,22,33,44}};
  126. Array.set(ints3, 0, ints3_1_row0_content);
  127. Array.set(ints3, 1, ints3_1_row1_content);
  128. System.out.println(Arrays.deepToString(ints3));
  129. //---------------------------------2---------------------------------
  130. int[][] ints2 = (int[][])Array.newInstance(int.class, 4,4);
  131. for (int i=0; i<4; i++) {
  132. ints2[i] = (int[]) Array.newInstance(int.class, i + 1);//重新创建每个第一位数组的长度
  133. }
  134. for(int[] array : ints2){
  135. for(int content : array){
  136. System.out.print(content + ",");
  137. }
  138. System.out.println();
  139. }
  140. //---------------------------------4---------------------------------
  141. int[][][] ints4 = new int[1][2][3];
  142. Class<?> clazzz = ints4.getClass();
  143. int dim = 0;
  144. while(clazzz.isArray()){
  145. dim ++;
  146. clazzz = clazzz.getComponentType();
  147. }
  148. System.out.println(dim);
  149. System.out.println(ints4.getClass().isArray());
  150. System.out.println(ints4.getClass().getComponentType().getName());
  151. System.out.println(ints4.getClass().getComponentType().getComponentType().getName());
  152. System.out.println(ints4.getClass().getComponentType().getComponentType().getComponentType().getName());
  153. //      System.out.println(ints2.getClass().getComponentType().getComponentType().getComponentType().getComponentType().getName());//java.lang.NullPointerException
  154. }
  155. }

5.注解 Annotation

  1. package com.java.new_features_jdk5;
  2. /**
  3. * 比较常用的注释:
  4. *  SuppressWarnings    指示应该在注释元素(以及包含在该注释元素中的所有程序元素)中取消显示指定的编译器警告。
  5. Deprecated          用"@Deprecated" 注释的程序元素,不鼓励程序员使用这样的元素,通常是因为它很危险或存在更好的选择。在使用不被赞成的程序元素或在不被赞成的代码中执行重写时,编译器会发出警告。
  6. Override            表示一个方法声明打算重写超类中的另一个方法声明。如果方法利用此注释类型进行注解但没有重写超类方法,则编译器会生成一条错误消息。
  7. * @author yuahan
  8. *
  9. */
  10. public class _Annotation {
  11. public static void main(String[] args) {
  12. }
  13. }

6.泛型

  1. package com.java.new_features_jdk5;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.List;
  5. /**
  6. * Java语言的泛型类似于C++中的模板. 但是这仅仅是基于表面的现象。Java语言的泛型基本上完全在编译器中实现的,由编译器执行类型检查和类型推断,然后生成普通的非泛型的字节码。 这种实现称为"擦除"(编译器使用泛型类型信息保证类型安全,然后在生成字节码之前将其清除)
  7. 需要注意的地方:
  8. *  1. 泛型不是协变的
  9. 协变:Java 语言中的数组是协变的(covariant),也就是说, 如果 Integer 扩展了 Number,那么不仅 Integer 是 Number,而且 Integer[] 也是 Number[],在要求Number[] 的地方完全可以传递或者赋予 Integer[]。
  10. 但是,泛型并不是协变的。  如果Number是Integer的超类型,但是,如果需要List<Integer>的时候, 并不容许传递List<Number>,它们并不等价。
  11. 不允许的理由很简单,这样会破坏要提供的类型安全泛型。
  12. 如:
  13. public static void main(String[] args) {
  14. List<Integer> list=new ArrayList<Integer>();
  15. List<Number> list2=list;//编译错误.
  16. list2.add(new Float(19.0f));
  17. }
  18. 2. 延迟构造
  19. 因为可以擦除功能,所以List<Integer>和List<String>是同一个类,编译器在编译List<V>的时候,只生成一个类。所以,运行时,不能区分List<Integer>和List<String>(实际上,运行时都是List,类型被擦除了),
  20. 用泛型类型参数标识类型的变量的构造就成了问题。运行时缺乏类型信息,这给泛型容器类和希望创建保护性副本的泛型类提出了难题。
  21. 3. 不能用通配符来帮助构造一个类(容器,数组等),因为根本不知道类型,不能确定该构造函数是否存在
  22. class Foo{
  23. public void doSomething(Set<?> set){
  24. Set<?> copy = new HashSet<?>(set);//编译出错,不能用通配符类型的参数调用泛型构造函数
  25. }
  26. }
  27. 或者
  28. class ArrayList<V>{
  29. V[] content;
  30. public ArrayList() {
  31. content = new V[10];//编译出错,不能实例化用类型参数表示的类型数组
  32. }
  33. }
  34. 不过可以用Object类帮助实现,不过看上去很不舒服。
  35. class Foo{
  36. public void doSomething(Set<?> set){
  37. Set<?> copy = new HashSet<Object>(set);
  38. }
  39. }
  40. 或者
  41. class ArrayList<V>{
  42. V[] content;
  43. public ArrayList() {
  44. content = (V[])new Object[10];
  45. }
  46. }
  47. 4. 擦除
  48. 因为泛型基本上都是在JAVA编译器中而不是运行库中实现的,所以在生成字节码的时候,差不多所有关于泛型类型的类型信息都被“擦除”了, 换句话说,编译器生成的代码与手工编写的不用泛型、检查程序类型安全后进行强制类型转换所得到的代码基本相同。
  49. 擦除意味着,一个类不能同时实现 Comparable<String>和Comparable<Number>,因为事实上,两者都在同一个接口中,指定同一个compareTo()方法。
  50. 5.  泛型的规则和限制
  51. (1  泛型的参数类型只能是类( class )类型,而不能是简单类型。
  52. 比如, <int> 是不可使用的。
  53. (2 可以声明多个泛型参数类型,比如 <T, P,Q…> ,同时还可以嵌套泛型,例如: <List<String>>.
  54. (3  泛型 的参数 类 型可以使用 extends 语 句,例如 <T extends superclass> 。
  55. (4  泛型的参数类型可以使用 super 语句,例如 < T super childclass> 。
  56. (5 泛型还可以使用通配符,例如 <? e xtends ArrayList>
  57. * @author yuahan
  58. *
  59. */
  60. public class _Generic <T> {
  61. public void array2Collection(T[] array, Collection<T> collection){
  62. if(array != null && collection != null){
  63. for(T content : array ){
  64. collection.add(content);
  65. }
  66. }
  67. }
  68. public static void main(String[] args) {
  69. _Generic<Integer> generic = new _Generic<Integer>();
  70. Integer[] array = new Integer[]{1,2,3,4};
  71. List<Integer> list = new ArrayList<Integer>();
  72. generic.array2Collection(array, list);
  73. System.out.println(list);
  74. }
  75. }

7.可变参数(Vararg)

  1. package com.java.new_features_jdk5;
  2. /**
  3. *
  4. * 可变参数可以解决代码冗余的问题。
  5. * 如:
  6. * public int max(int i, int j);
  7. * public int max(int i, int j, int k);
  8. * 可以简化成
  9. * public int max(int... num);
  10. *
  11. * 可以将可变参数视为长度可变的数组。不过需要注意以下问题 :
  12. *  1. 变长参数一定要放在最后面
  13. max(int ... nums, int temp)// error
  14. *  2. 可变参数不能与数组参数并存
  15. max(int[] nums)//error
  16. * @author yuahan
  17. *
  18. */
  19. public class _Var_Arg {
  20. public static int max(int ... nums){
  21. int max = Integer.MIN_VALUE;
  22. for(int num : nums){
  23. if(num >= max){
  24. max = num;
  25. }
  26. }
  27. return max;
  28. }
  29. public static void main(String[] args) {
  30. int max1 = _Var_Arg.max(1,2,3,4,5);
  31. int max2 = _Var_Arg.max(new int[]{1,2,3,4,5});
  32. System.out.println(max1);
  33. System.out.println(max2);
  34. }
  35. }

8.格式化输入输出 
不太喜欢c的输出格式,没有进行尝试。

最新文章

  1. Java数据结构——树的三种存储结构
  2. [转]给 C# 开发者的代码审查清单
  3. remove 清除binlog
  4. iOS梦想之路-最新消息
  5. shell脚本监控Flume输出到HDFS上文件合法性
  6. js 实现文字列表滚动效果
  7. CSS 实现:两栏布局(等宽布局)
  8. 网络流的一个很厉害的ppt
  9. Loading Cargo
  10. easyUI的doCellTip 就是鼠标放到单元格上有个提示的功能
  11. Android单元測试之JUnit
  12. thinking in java 随笔
  13. LightOJ DNA Prefix(字典树+dfs)
  14. centos7搭建本地 Remix
  15. mongoDB 文档操作_查
  16. Python 内置库 sys用法
  17. Html 文字排版
  18. Ubuntu安装搜狗sougou输入法
  19. ionic 版本内更新问题汇总
  20. Confluence 6 空间标识

热门文章

  1. Ubuntu实用快捷键
  2. 动态更新Toolbar Menu以及Menu中同时显示文字和图标
  3. 来吧,给你的Winform列表控件画个妆
  4. VS2010常用插件介绍
  5. JSON-JObject
  6. ETL Pentaho Data Integration (Kettle) 插入/更新 问题 etl
  7. SQL语言笔记
  8. POJ 3553 Task schedule
  9. 你应该了解的jquery 验证框架
  10. Appium —— desired_capabilities详解