网管联盟 | 网管论坛 | 网管u家 | 网管博客 | 网管软件 | 网管求职 | 小游戏 | 网管搜索 | 网管原创 | 网管聚合 | 网管读摘 | 网管焦点 | 世界素材 | 会员投稿 | 会员中心 
中国网管联盟
Windows Linux Cisco 网络技术 数据库 黑客攻防 DotNet Java PHP 认证 新闻资讯 服务器 存储资讯 网络设备 网管学堂 技术专题 焦点 网吧频道
 当前位置: > bitsCN.com > JAVA > 新手入门 > JDK > JDK5.0的11个主要新特征一览  

JDK5.0的11个主要新特征一览

2008-06-14  作者:bitsCN整理  来源:中国网管联盟  点评 投稿 收藏

    1           泛型(Generic)
    1.1          说明
    增强了java的类型安全,可以在编译期间对容器内的对象进行类型检查,在运行期不必进行类型的转换。而在j2se5之前必须在运行期动态进行容器内对象的检查及转换
    减少含糊的容器,可以定义什么类型的数据放入容器 网管网www.bitscn.com

    ArrayList<Integer> listOfIntegers; // <TYPE_NAME> is new to the syntax
    Integer integerObject;
    listOfIntegers = new ArrayList<Integer>(); // <TYPE_NAME> is new to the syntax
    listOfIntegers.add(new Integer(10)); // 只能是Integer类型
    integerObject = listOfIntegers.get(0); // 取出对象不需要转换 网管网www.bitscn.com


    1.2          用法

网管联盟bitsCN@com

    声明及实例化泛型类:
    HashMap<String,Float> hm = new HashMap<String,Float>();
    //不能使用原始类型
    GenList<int> nList = new GenList<int>();  //编译错误 网管联盟bitsCN_com


    J2SE 5.0目前不支持原始类型作为类型参数(type parameter) 网管联盟bitsCN_com

    定义泛型接口:
    public interface GenInterface<T> {
        void func(T t);
    }

网管联盟bitsCN_com


    定义泛型类:
    public class ArrayList<ItemType> { ... }
    public class GenMap<T, V> { ... }
    例1:
    public class MyList<Element> extends LinkedList<Element>
    {
           public void swap(int i, int j)
           {
                  Element temp = this.get(i);
                  this.set(i, this.get(j));
                  this.set(j, temp);
           } 中国网管论坛bbs.bitsCN.com

           public static void main(String[] args)
           {
                  MyList<String> list = new MyList<String>();
                  list.add("hi");
                  list.add("andy");
                  System.out.println(list.get(0) + " " + list.get(1));
                  list.swap(0,1);
                  System.out.println(list.get(0) + " " + list.get(1));
           }

网管联盟bitsCN_com


    } 网管联盟bitsCN_com


    例2:
    public class GenList <T>{
           private T[] elements;
           private int size = 0;
           private int length = 0;

网管u家u.bitscn@com

           public GenList(int size) {
                  elements = (T[])new Object[size];
                  this.size = size;
           } 网管联盟bitsCN_com

           public T get(int i) {
                  if (i < length) {
                         return elements[i];
                  }
                  return null;
           }

网管下载dl.bitscn.com

           public void add(T e) {
                  if (length < size - 1)
                         elements[length++] = e;
           }
    } 网管bitscn_com


    泛型方法:
    public class TestGenerics{
           public <T> String getString(T obj) { //实现了一个泛型方法
                  return obj.toString();
           } 网管网www.bitscn.com

           public static void main(String [] args){
                  TestGenerics t = new TestGenerics();
                  String s = "Hello";
                  Integer i = 100;
                  System.out.println(t.getString(s));
                  System.out.println(t.getString(i));
                  }
    } 网管网www_bitscn_com


    1.3          受限泛型
      受限泛型是指类型参数的取值范围是受到限制的. extends关键字不仅仅可以用来声明类的继承关系, 也可以用来声明类型参数(type parameter)的受限关系.例如, 我们只需要一个存放数字的列表, 包括整数(Long, Integer, Short), 实数(Double, Float), 不能用来存放其他类型, 例如字符串(String), 也就是说, 要把类型参数T的取值泛型限制在Number极其子类中.在这种情况下, 我们就可以使用extends关键字把类型参数(type parameter)限制为数字
    示例

网管论坛bbs_bitsCN_com


    public class Limited<T extends Number> {
           public static void main(String[] args) {
                  Limited<Integer> number;   //正确
                  Limited<String> str;       //编译错误
           }
    }

网管网www.bitscn.com


    1.4          泛型与异常

中国网管联盟bitsCN.com

    类型参数在catch块中不允许出现,但是能用在方法的throws之后。例:
    import java.io.*;
    interface Executor<E extends Exception> {
           void execute() throws E;
    }

网管u家u.bitscn@com

    public class GenericExceptionTest {
           public static void main(String args[]) {
                  try {
                         Executor<IOException> e = new Executor<IOException>() {
                                public void execute() throws IOException{
                                       // code here that may throw an
                                       // IOException or a subtype of 网管bitscn_com
                                       // IOException
                                }
                                };
                         e.execute();
                  } catch(IOException ioe) {
                         System.out.println("IOException: " + ioe); 网管下载dl.bitscn.com
                         ioe.printStackTrace();
                  }
           }
    }

网管下载dl.bitscn.com


    1.5          泛型的通配符"?"

网管下载dl.bitscn.com

    "?"可以用来代替任何类型, 例如使用通配符来实现print方法。
    public static void print(GenList<?> list) {})
    1.6          泛型的一些局限型
    不能实例化泛型
    T t = new T(); //error
    不能实例化泛型类型的数组
    T[] ts= new T[10];   //编译错误
    不能实例化泛型参数数
    Pair<String>[] table = new Pair<String>(10); // ERROR
    类的静态变量不能声明为类型参数类型
    public class GenClass<T> {
         private static T t;    //编译错误
    }
    泛型类不能继承自Throwable以及其子类
    public GenExpection<T> extends Exception{}    //编译错误
    不能用于基础类型int等
    Pair<double> //error

网管联盟bitsCN_com


    Pair<Double> //right
    2           增强循环(Enhanced for Loop)
    旧的循环
    LinkedList list = new LinkedList();
    list.add("Hi");
    list.add("everyone!");
    list.add("Was");
    list.add("the");
    list.add("pizza");
    list.add("good?");
    for (int i = 0; i < list.size(); i++)
           System.out.println((String) list.get(i));
    //或者用以下循环
    //for(Iterator iter = list.iterator(); iter.hasNext(); ) {
    //Integer stringObject = (String)iter.next();
    // ... more statements to use stringObject...
    /

TAGs
 上一篇:关于JDK1.5中的新特性   下一篇:Spring RMI 支持简单介绍
JDK5.0的11个主要新特征一览 评论:
loading.. 评论加载中…
评论:请自觉遵守互联网相关政策法规,评论不得超过250字。

验证码: 注册用户
本类热门排行:
最新推荐文章:
网管论坛交流: