ITeye博客

jdk5.0新特性

hyhandylit · 2010-01-27 15:13 · J2SE · 编程语言 · C · Java · C++ · C# · JDK · Markdown

d1:泛型(Generics) 可以接受不同的类型。 按照不同类型生成相应的对象 package com.hyh.jdk; public class Generic { private T foo; public T getFoo() { return foo; } public void setFoo(T foo) { this.foo = foo; } public static void main(String[] args){ Generic foo1= new Generic(); Generic foo2= new Generic(); foo1.setFoo(new Boolean(true)); Boolean b = foo1.getFoo(); foo2.setFoo(new Integer(10)); Integer i = foo2.getFoo(); //不同类型间不能赋值 foo1 = foo2; System.out.println(foo1); } } 2:泛型继承自某个接口 package com.hyh.jdk; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class ListGenericFoo> { private T[] fooArray; public T[] getFooArray() { return fooArray; } public void setFooArray(T[] fooArray) { this.fooArray = fooArray; } public static void main(String[] args) { ListGenericFoo> foo1 = new ListGenericFoo>(); ListGenericFoo> foo2 = new ListGenericFoo>(); } } 3:用map统计参数的个数 package com.hyh.jdk; import java.util.Map; import java.util.TreeMap; public class TongJi { public static void main(String[] args) { // ab ab cd cd c d a b ad Map map = new TreeMap(); for (String word : args) { Integer in = map.get(word); //得到对应键的值 map.put(word, in == null ? 1 : in+1); } System.out.println(map); } } 4:自动类型转化,int在-127--128用基本类型比较,超出范围则比较其包装 类 尽量避免容易混淆。 package com.hyh.jdk; import java.util.Map; import java.util.TreeMap; public class TongJi { public static void main(String[] args) { Integer i1 = 100; Integer i2 = 100; if (i1 == i2) { System.out.println("===="); } else { System.out.println("!==="); } Integer a = 200; Integer b = 200; if (a == b) { System.out.println("===="); } else { System.out.println("!==="); } } }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。