泛型是JAVA的核心特型之一,我们先看一个例子:

没有使用泛型前,如下:

import java.util.ArrayList;
import java.util.List; public class GenericsStu {
public static void main(String[] args) { List list = new ArrayList();
String name = "gavin";
Integer age = 5;
list.add(name);
list.add(age); for (Object obj : list) {
String str = (String) obj; // Exception in thread "main"
// java.lang.ClassCastException:
// java.lang.Integer cannot be cast to
// java.lang.String
}
}
}

使用泛型后如下:

import java.util.ArrayList;
import java.util.List; public class GenericsStu {
public static void main(String[] args) { List<String> list = new ArrayList<String>();
String name = "gavin";
Integer age = 5;
list.add(name);
list.add("hello");
list.add(age);//这个在编译时就提示类型不匹配,如果用的是eclipse,会及时显示错误,避免了运行时异常。 for (String str : list) {
System.out.println(str); }
}
}

泛型也可以用于Class,例如:

使用泛型前,是这样的:

 public class GenericsStu {

     private Object t;

     public Object get() {
return t;
} public void set(Object t) {
this.t = t;
} public static void main(String args[]){
GenericsStu type = new GenericsStu();
type.set(2);
String str = (String) type.get(); //Exception in thread "main" java.lang.ClassCastException:
//java.lang.Integer cannot be cast to java.lang.String
System.out.println(str);
}
}

使用泛型后,是这样的:

 public class GenericsStu<T> {

     private T t;

     public T get(){
return this.t;
} public void set(T t){
this.t=t;
} public void print(){
System.out.println(t);
} public static void main(String args[]){
GenericsStu<String> type = new GenericsStu<String>();
type.set("gavin");
type.print();//Output is:gavin
type.set(2); //编译就不通过,避免了运行时java.lang.ClassCastException
type.print(); }
}

008 The Generics In JAVA的更多相关文章

  1. thinking in java Generics Latent typing

    The beginning of this chapter introduced the idea of writing code that can be applied as generally a ...

  2. Java ConcurrentHashMap Example and Iterator--转

    原文地址:http://www.journaldev.com/122/java-concurrenthashmap-example-iterator#comment-27448 Today we wi ...

  3. Thinking in Java——笔记(15)

    Generics The term "generic" means "pertaining or appropriate to large groups of class ...

  4. Java深度历险(五)——Java泛型

      作者 成富 发布于 2011年3月3日 | 注意:QCon全球软件开发大会(北京)2016年4月21-23日,了解更多详情!17 讨论 分享到:微博微信FacebookTwitter有道云笔记邮件 ...

  5. 在Android中使用Java 8的lambda表达式

    作为一名Java开发者,或许你时常因为缺乏闭包而产生许多的困扰.幸运的是:Java's 8th version introduced lambda functions给我们带来了好消息;然而,这咩有什 ...

  6. Effective Java Index

    Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...

  7. [CareerCup] 14.4 Templates Java模板

    14.4 Explain the difference between templates in C++ and generics in Java. 在Java中,泛式编程Generic Progra ...

  8. java基础之 泛型

    泛型(Generic type 或者generics)是对 Java 语言的类型系统的一种扩展,以支持创建可以按类型进行参数化的类.可以把类型参数看作是使用参数化类型时指定的类型的一个占位符,就像方法 ...

  9. Java之泛型练习

    package cn.itcast.generics; import java.util.Comparator; import java.util.Iterator; import java.util ...

随机推荐

  1. HackerRank "The Indian Job"

    A sly knapsack problem in disguise! Thanks to https://github.com/bhajunsingh/programming-challanges/ ...

  2. Eclipse中添加web dynamic project

    因为我的eclipse版本是kepler service release 2,所以我用了这个链接,http://download.eclipse.org/releases/helios/ 参考链接:  ...

  3. 水晶报表连接Oracle做数据报表笔记

    首先,新建一个水晶报表的文件,这个时候要给这个报表文件绑定一个oracle数据源, 选择右侧菜单的这个东西,选择“数据库专家”,打开之后是这么一个界面: 选择建立新连接: 这个地方最关键,也是我为什么 ...

  4. 51nod 1348 乘积之和

    用(r-l+2)维向量f[l,r]表示区间[l,r]内选i个数(0<=i<=r-l+1)相乘的所有方案之和,可以发现f[l,r]=f[l,m]*f[m+1,r],题目模数100003较小, ...

  5. elixir学习

    安装 brew install elixir atom配置 language-elixir atom-elixir elixir的shell iex :erlang.system_info(:otp_ ...

  6. Windows 7无线网卡启用wifi共享蓝屏!

    我的笔记本是联想Y460P,装的是Windows 7 Ultiame(x64)系统,通过设置笔记本的无线(Intel WiFi Link 1000 BGN)搭建Wifi环境并共享,使手机能够通过笔记本 ...

  7. [内核同步]自旋锁spin_lock、spin_lock_irq 和 spin_lock_irqsave 分析

    转自:http://blog.csdn.net/wh_19910525/article/details/11536279 自旋锁的初衷:在短期间内进行轻量级的锁定.一个被争用的自旋锁使得请求它的线程在 ...

  8. linux系统中实现mongodb3.0.5数据库自动备份

    最近两天,因公司业务需要,要定期备份mongodb数据库中的数据. 查了很多资料后,发现mongodb似乎并没有自带的定时备份功能,于是只好转移目标到linux系统的定时任务上,于是学习并使用了cro ...

  9. Eclipse:启动时提示"Failed to load the JNI shared library"的解决方案

    今天打开Eclipse,弹出提示框"Failed to load the JNI shared library" 原因1:给定目录下jvm.dll不存在. 对策:(1)重新安装jr ...

  10. (C# 基础) Datatable

    增加一行: DataTable measurements = new DataTable(); measurements.Columns.Add("StationTestName" ...