1.静态导入方法
- package com.java.new_features_jdk5;
-
- public class _Static_Import {
- public static void main(String[] args) {
-
- }
- }
2.新增加的for循环
- package com.java.new_features_jdk5;
-
- import java.util.ArrayList;
- import java.util.List;
-
- public class _For {
- @SuppressWarnings("serial")
- public static void main(String[] args) {
- int[] array = {1,2,3,4,5,6,7,8,9,10};
- for(int num : array){
- System.out.print(num + " ");
- }
-
- System.out.println();
-
- List<Integer> list = new ArrayList<Integer>(){{
- this.add(1);
- this.add(2);
- this.add(3);
- this.add(4);
- this.add(5);
- this.add(6);
- this.add(7);
- this.add(8);
- this.add(9);
- this.add(10);
- }};
-
- for(int num : list){
- System.out.print(num + " ");
- }
- }
- }
3.枚举 Enum
- package com.java.new_features_jdk5;
-
- public enum _Enum {
- Better(90),
- Good(80),
- Ok(70),
- Bad(60),
- Worse;
-
- private int value;
-
- private _Enum() {
- this.value = 30;
- }
-
- private _Enum(int value) {
- this.value = value;
- }
-
- public int getValue() {
- return value;
- }
-
- public void setValue(int value) {
- this.value = value;
- }
-
- public static _Enum[] getEnumValues(){
- return _Enum.values();
- }
-
- public static void _ValuesOf(){
-
- _Enum Better = _Enum.valueOf("Better");
- System.out.println(Better);
- }
-
-
- public static void main(String[] args) {
- for(_Enum mark : _Enum.getEnumValues()){
- switch(mark){
- case Better:
- System.out.println(_Enum.Better);
- break;
- case Good:
- System.out.println(_Enum.Good);
- break;
- case Ok:
- System.out.println(_Enum.Ok);
- break;
- case Bad:
- System.out.println(_Enum.Bad);
- break;
- case Worse:
- System.out.println(_Enum.Worse);
- break;
- }
- }
-
- _Enum._ValuesOf();
-
- System.out.println(_Enum.Better.getValue());
- }
- }
4.反射 Reflect
- package com.java.new_features_jdk5;
-
- import java.lang.reflect.Array;
- import java.lang.reflect.Constructor;
- import java.lang.reflect.Field;
- import java.lang.reflect.Method;
- import java.lang.reflect.Modifier;
- import java.util.Arrays;
-
- public class _Reflect {
- private int id;
- private String name;
- private String[] hobbies;
-
- public _Reflect(){}
-
- public _Reflect(int id){
- this.id = id;
- }
-
- public _Reflect(int id, String name, String[] hobbies) {
- super();
- this.id = id;
- this.name = name;
- this.hobbies = hobbies;
- }
-
- public int getId() {
- return id;
- }
-
- public void setId(int id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String[] getHobbies() {
- return hobbies;
- }
-
- public void setHobbies(String[] hobbies) {
- this.hobbies = hobbies;
- }
-
- public static void main(String[] args) throws Exception{
-
- System.out.println(_Reflect.class.getSimpleName());
- System.out.println(_Reflect.class.getName());
- System.out.println(_Reflect.class.getPackage());
- System.out.println(_Reflect.class.getSuperclass().getName());
- System.out.println(int[].class.getName());
- System.out.println(_Reflect[].class.getName());
-
-
- Method[] methods = _Reflect.class.getMethods();
- for(Method method : methods){
- System.out.println(method + ": " +method.getDeclaringClass().getName());
- }
-
-
-
- System.out.println(Comparable.class.isInterface());
- System.out.println(int.class.isPrimitive());
- System.out.println(int[].class.isArray());
-
-
-
- System.out.println(Modifier.isPublic(_Reflect.class.getModifiers()));
-
- Class<?>[] classes = null;
- System.out.println(Modifier.isPublic(_Reflect.class.getMethod("getId",classes).getModifiers()));
-
-
-
- System.out.println(Number.class.isAssignableFrom(Integer.class));
- System.out.println(Number.class.isInstance(1));
-
-
-
- _Reflect _Reflect = new _Reflect();
- System.out.println(_Reflect.getId());
- System.out.println(_Reflect.getName());
- System.out.println(Arrays.toString(_Reflect.getHobbies()));
-
- Field[] fields = _Reflect.class.getDeclaredFields();
- for(Field field : fields){
- if(field.getType() == int.class){
- field.setAccessible(true);
- field.setInt(_Reflect, 1);
- }else if(field.getType() == String.class){
- field.setAccessible(true);
- field.set(_Reflect, "1");
- }else if(field.getType() == String[].class){
- field.setAccessible(true);
- field.set(_Reflect, new String[]{"1","1"});
- }
- }
-
- System.out.println(_Reflect.getId());
- System.out.println(_Reflect.getName());
- System.out.println(Arrays.toString(_Reflect.getHobbies()));
-
-
- Constructor<_Reflect> constructor = _Reflect.class.getConstructor(new Class[]{int.class,String.class,String[].class});
- _Reflect _reflect = constructor.newInstance(new Object[]{1,"1",new String[]{"1","1"}});
- System.out.println(_reflect.getId());
- System.out.println(_reflect.getName());
- System.out.println(Arrays.toString(_Reflect.getHobbies()));
-
-
- Class<?> clazz = Class.forName("com.java.new_features_jdk5._Reflect");
- _Reflect clazzes = (_Reflect)clazz.newInstance();
- System.out.println(clazzes.getId());
- System.out.println(clazzes.getName());
- System.out.println(Arrays.toString(clazzes.getHobbies()));
-
-
-
-
-
-
- int[] ints0 = (int[])Array.newInstance(int.class, 3);
- Array.setInt(ints0, 0, 0);
- Array.setInt(ints0, 1, 1);
- Array.setInt(ints0, 2, 2);
- System.out.println(Arrays.toString(ints0));
-
-
-
- int[][][] ints3 = (int[][][])Array.newInstance(int.class,2,3,4);
-
- System.out.println(ints3.length);
- System.out.println(ints3[0].length);
- System.out.println(ints3[0][0].length);
-
- int[][] ints3_1_row0_content = new int[][]{{1,2,3,4},{1,2,3,4},{1,2,3,4}};
- int[][] ints3_1_row1_content = new int[][]{{11,22,33,44},{11,22,33,44},{11,22,33,44}};
- Array.set(ints3, 0, ints3_1_row0_content);
- Array.set(ints3, 1, ints3_1_row1_content);
- System.out.println(Arrays.deepToString(ints3));
-
-
-
- int[][] ints2 = (int[][])Array.newInstance(int.class, 4,4);
- for (int i=0; i<4; i++) {
- ints2[i] = (int[]) Array.newInstance(int.class, i + 1);
- }
- for(int[] array : ints2){
- for(int content : array){
- System.out.print(content + ",");
- }
- System.out.println();
- }
-
-
-
- int[][][] ints4 = new int[1][2][3];
- Class<?> clazzz = ints4.getClass();
- int dim = 0;
- while(clazzz.isArray()){
- dim ++;
- clazzz = clazzz.getComponentType();
- }
- System.out.println(dim);
-
- System.out.println(ints4.getClass().isArray());
- System.out.println(ints4.getClass().getComponentType().getName());
- System.out.println(ints4.getClass().getComponentType().getComponentType().getName());
- System.out.println(ints4.getClass().getComponentType().getComponentType().getComponentType().getName());
-
- }
- }
5.注解 Annotation
- package com.java.new_features_jdk5;
-
- public class _Annotation {
- public static void main(String[] args) {
-
- }
- }
6.泛型
- package com.java.new_features_jdk5;
-
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.List;
-
- public class _Generic <T> {
- public void array2Collection(T[] array, Collection<T> collection){
- if(array != null && collection != null){
- for(T content : array ){
- collection.add(content);
- }
- }
- }
- public static void main(String[] args) {
- _Generic<Integer> generic = new _Generic<Integer>();
- Integer[] array = new Integer[]{1,2,3,4};
- List<Integer> list = new ArrayList<Integer>();
- generic.array2Collection(array, list);
- System.out.println(list);
- }
- }
7.可变参数(Vararg)
- package com.java.new_features_jdk5;
-
- public class _Var_Arg {
-
- public static int max(int ... nums){
- int max = Integer.MIN_VALUE;
- for(int num : nums){
- if(num >= max){
- max = num;
- }
- }
- return max;
- }
-
-
- public static void main(String[] args) {
- int max1 = _Var_Arg.max(1,2,3,4,5);
- int max2 = _Var_Arg.max(new int[]{1,2,3,4,5});
- System.out.println(max1);
- System.out.println(max2);
- }
- }
8.格式化输入输出
不太喜欢c的输出格式,没有进行尝试。
java5 新特性
原文:http://www.cnblogs.com/tian830937/p/4899437.html