java核心day01

包装类

Java数据类型分为基本数据类型和引用数据类型

  • 基本数据类型《==》包装类:装箱和拆箱

    • 自动装箱,拆箱

    • 基本数据类型----->包装类

  • 自动装箱

    • int类型转换成Integer

           int a1 = 12;
       //    自动装箱
           Integer a2 = a1;

  • 自动拆箱

    •  Integer integer = new Integer(12);
        /*自动拆箱*/
       //    int  a= integer;

  • 转换成字符串的三种方法

      int a1 = 12;
     //    自动装箱
         Integer a2 = a1;
         String b = Integer.toString(a2);
      int a1 = 12;
     //    自动装箱
         Integer a2 = a1;
         String b = ""+a2;
         int a1 = 12;
     //    自动装箱
         Integer a2 = a1;
         String s = String.valueOf(a1);
    • 使用静态方法toString

    • 使用字符串拼接方式

    • 使用String.valueOf()

泛型

  • 可以在类或者方法中预知的使用类型

  • 泛型的好处:使用类型与预支的类型不同时会编译错误、避免了类型强转的麻烦。

  • 可以在类或方法中预支地使用未知的类型。

  • 型是数据类型的一部分,我们将类名与泛型合并一起看做数据类型。

  • 泛型,用来灵活地将数据类型应用到不同的类、方法、接口当中。将数据类型作为参数进行传递

泛型的定义与使用

修饰符 class 类名<代表泛型的变量> { }

  • 在使用泛型的时候再去调用他的格式

  • 我们在集合中会大量使用到泛型,这里来完整地学习泛型知识。

  • 泛型,用来灵活地将数据类型应用到不同的类、方法、接口当中。将数据类型作为参数进行传递。

  • 在没有使用泛型区定义集合Collection时,可以多存储多种元素

 class ArrayList<E>{
 public boolean add(E e){ }
 ​
 public E get(int index){ }
 ....
 }
 ​

使用泛型: 即什么时候确定泛型。

在创建对象的时候确定泛型

 ArrayList<String> list = new ArrayList<String>();

自定义泛型:举例

 public class MyGenericClass<MVP> {
 //没有MVP类型,在这里代表 未知的一种数据类型 未来传递什么就是什么类型
 private MVP mvp;
 ​
 public void setMVP(MVP mvp) { 
 this.mvp = mvp;
 }
 ​
 public MVP getMVP() { 
 return mvp;
 }
 }
 ​

使用

 public class GenericClassDemo {
 public static void main(String[] args) {
 // 创建一个泛型为String的类
 MyGenericClass<String> my = new MyGenericClass<String>();
 // 调 用 setMVP my.setMVP("大胡子登登");
 // 调用getMVP
 String mvp = my.getMVP(); System.out.println(mvp);
 //创建一个泛型为Integer的类
 MyGenericClass<Integer> my2 = new MyGenericClass<Integer>(); my2.setMVP(123);
 Integer mvp2 = my2.getMVP();
 }
 }
 ​

含有泛型的方法

定义格式:

修饰符 <代表泛型的变量> 返回值类型 方法名(参数){ }

例如:

 public class MyGenericMethod { 
 public <MVP> void show(MVP mvp) {
 System.out.println(mvp.getClass());
 }
 ​
 public <MVP> MVP show2(MVP mvp) { 
 return mvp;
 }
 }
 ​

使用格式:调用方法时,确定泛型的类型

 public class GenericMethodDemo {
 public static void main(String[] args) {
 // 创建对象
 MyGenericMethod mm = new MyGenericMethod();
 // 演示看方法提示mm.show("aaa"); mm.show(123);
 mm.show(12.45);
 }
 }
 ​

含有泛型的接口

定义格式:

iterface接口名<代表泛型的变量> { }

 public interfaceMyGenericInterface<E>{ 
 public abstract void add(E e);
 ​
 public abstract E getE();
 }
 ​

使用格式:

定义类时确定泛型的类型

 public class MyImp1 implements MyGenericInterface<String> { 
 @Override
 public void add(String e) {
 // 省略...
 }
 ​
 @Override
 public String getE() { 
 return null;
 }
 }
 ​

此时,泛型E的值就是String类型。

始终不确定泛型的类型,直到创建对象时,确定泛型的类型

 public class MyImp2<E> implements MyGenericInterface<E> { 
 @Override
 public void add(E e) {
 // 省略...
 }
 ​
 @Override
 public E getE() { 
 return null;
 }
 }
 ​

确定泛型:

 ​
 * 使用
 */
 public class GenericInterface {
 public static void main(String[] args) { 
 MyImp2<String> my = new MyImp2<String>(); my.add("aa");
 }
 }
 ​

泛型通配符

当使用泛型类或者接口时,传递的数据中,泛型类型不确定,可以通过通配符<?>表示。但是一旦使用泛型的通配符后,只能使用Object类中的共性方法,集合中元素自身方法无法使用。

通配符基本使用

泛型的通配符:

  • 不知道使用什么类型来接收的时候,

    • 此时可以使用?,?表示未知通配符。

    • 此时只能接受数据,不能往该集合中存储数据。

 public static void main(String[] args) { 
 Collection<Intger> list1 = new ArrayList<Integer>(); getElement(list1);
 Collection<String> list2 = new ArrayList<String>(); getElement(list2);
 }
 public static void getElement(Collection<?> coll){}
 //?代表可以接收任意类型
 ​

通配符高级使用-- 受限泛型

之前设置泛型的时候,实际上是可以任意设置的,只要是类就可以设置。但是在JAVA的泛型中可以指定一个泛型的上限下限

  • 泛型的上限

    • 格式: 类型名称 <? extends 类 > 对象名称

    • 意义: 只能接收该类型及其子类

  • 泛型的下限

    • 格式: 类型名称 <? super 类 > 对象名称

    • 意义: 只能接收该类型及其父类型

例如:

 public static void main(String[] args) { 
 Collection<Integer> list1 = new ArrayList<Integer>(); Collection<String> list2 = new ArrayList<String>(); Collection<Number> list3 = new ArrayList<Number>(); Collection<Object> list4 = new ArrayList<Object>();
 ​
 getElement(list1); getElement(list2);//报错getElement(list3); getElement(list4);//报错
 ​
 getElement2(list1);//报错getElement2(list2);//报错getElement2(list3); getElement2(list4);
 ​
 }
 // 泛型的上限:此时的泛型?,必须是Number类型或者Number类型的子类
 public static void getElement1(Collection<? extends Number> coll){}
 // 泛型的下限:此时的泛型?,必须是Number类型或者Number类型的父类
 public static void getElement2(Collection<? super Number> coll){}
 ​
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值