In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today Xiaobian to share with you Java generics and wrapper class instance analysis related knowledge points, detailed content, clear logic, I believe most people are still too familiar with this knowledge, so share this article for your reference, I hope you have read this article after harvest, let's take a look at it.
1 What is generic?
The essence of generics is to parameterize types (to control the types specifically restricted by parameters through different types specified by generics without creating new types).
Consider the following example:
Arrays, as we learned before, can only hold elements of a specified type. For example: int[] array=new int[10];String[] array=new String[10]; and Object class is the parent class of all classes, so can we create Obj array?
class Myarray{ public Object[] array=new Object[10]; public void setVal(int pos,Object val){ this.array[pos]=val; } public Object getPos(int pos){ return this.array[pos]; }}public class TestDemo{ public static void main(String[] args) { Myarray myarray=new Myarray(); myarray.setVal(1,0); myarray.setVal(2,"shduie");//String can also be stored String ret=(String)myarray.getPos(2);//although we know it is a string type, we still have to force type conversion System.out.println(ret); }}
After implementing the above code, we found that:
Any type of data can be stored
Subscript 2 is a string, but must be cast.
Generics are derived from this, and the purpose of generics is to specify what type of object the current container should hold, leaving the compiler to check for itself.
2. Generic syntax
generic class name
< 类型形参列表>{
//这里可以使用类型参数
}
泛型的使用:
泛型类 变量名=new 泛型类(构造方法实参)
MyArray list=new MyArray();
【注】
类型后的代表占位符,表示当前类是一个泛型类
在实例化泛型时,中不能是简单的类型,需要是包装类
不参与泛型的类型组成
不能new泛型类型的数组
使用泛型不需要进行强制类型转换
一个简单的泛型:
//此处T可以随便写为任意标识,常见的如T、E、K、V等形式的参数常用于表示泛型//在实例化泛型类时,必须指定T的具体类型public class Test{ //key这个成员变量的类型为T,T的类型由外部指定 private T key; public Test(T key) { //泛型构造方法形参key的类型也为T,T的类型由外部指定 this.key = key; } public T getKey(){ //泛型方法getKey的返回值类型为T,T的类型由外部指定 return key; }}
擦除机制:编译时会将中的类型擦除掉,所以中的东西不参与类型的组成。会将T擦除为Object。
为什么不能实例化泛型类型的数组?
数组和泛型之间的一个重要区别是它们如何强制执行类型检查。数组在运行时存储和检查类型信息,而泛型是在编译时检查类型错误。
返回的Object数组里面,可能存放着任何类型的数据,如string,通过int类型的数组来接收,编译器认为是不安全的。
3、泛型的上界
语法:
class 泛型类名称{
}
例:
public class MyArray{} //E只能是Number或Number的子类
public class MyArray{}
//E一定实现了Comparable接口的类
【注】没有指定边界的E,可以看作 E extends Object
4、通配符
? 用于在泛型的使用,即为通配符。通配符用来解决反泛型无法协变的问题。
如下两段代码:
代码一:public static void printList1(ArrayList list){ for(T x:list){ System.out.println(x); }} 代码二:public static void printList2(ArrayList list){ for(Object x:list){ System.out.println(x); }}
代码2中使用了通配符,和代码1相比,此时传入代码1的具体是什么数据类型,我们是不清楚的。
(1)通配符的上界
语法:
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.