基本数据类型、包装类、String6种转换形式

包装类
JAVA提供了俩种数据类型:基本数据类型和引用数据类型
引用数据类型,其实就是各种JAVA jdk提供的类
包装类只要用在泛型< E >中


很多时候,我们需要创建对象来解决问题,如ArrayList< E >,其中E必须是一个引用数据类型,如果想使用基本数据类型,就必须使用基本数据类型对应的引用数据类型,即包装类。
包装类在将基本数据类型包装为引用数据类型的同时,也定义了对基本数据类型操作的方法
 

    @Test
    public void cc(){
    //包装类 Integer Float 。。。
    //String -->Int
    //String-->Integer
        String a="88";
        int i = Integer.parseInt(a);
        System.out.println(i);

        String b="88";
        Integer j = new Integer(b);
        System.out.println(j);



    //int-->Integer
    //int-->String
      int a1=88;
      Integer i1 = new Integer(a1);
      System.out.println(i1);

      int b1=88;
      String j1 = String.valueOf(b1);
      System.out.println(i1);



    //Integer-->int
    //Integer-->String
    Integer a3=new Integer(88);//或者new Integer("88");
    int i2 = a3.intValue();
    System.out.println(i2);

    Integer B3=new Integer(88);//或者new Integer("88");
    String s = B3.toString();
    System.out.println(s);
    }
    
    
    //3个对象直接一共有6种转换方法  
    // 其中 Integer 用了三种 Integer.parseInt()  Integer.intvalue()  Integer.toString()
    //String 用了一种 String.ValueOf()
    //后面会有新特性
}
   1. java.lang包中的Integer类是我们比较常用的类,比如以下代码: 
                      Integer a=new Integer(1)

                      Integer a=Integer.valueOf(1);
    
    两个都是得到一个Integer对象;但是Inter.valueOf  的效率更高,Inter.valueOf  用到了缓存机制。
    
 其中Integer.valueOf方法代码如下:

 1. public static Integer valueOf(int i) {
         final int offset = 128;
         if (i >= -128 && i <= 127) { // must cache 
         return IntegerCache.cache[i + offset];
        }
            return new Integer(i);
        }
        在这个类中使用到了一个辅助类IntegerCache,IntegerCache的源码为:
        
        private static class IntegerCache {
        private IntegerCache(){}
        static final Integer cache[] = new Integer[-(-128) + 127 + 1];
        static {
         for(int i = 0; i < cache.length; i++)
         cache[i] = new Integer(i - 128);
               }
          }

  可以看到在IntegerCache中已经初始化了cache数组。所以,要注意的是,    new Integer返回的永远是不同的对象,但是当整数范围在-128<i<=127时,Integer.valueOf返回的是同一个对象。

看下面代码:Integer a=new Integer(1);   
    Integer b=new Integer(1);   
      
    //整数范围在-128到127之间,返回的是同一个对象   
    Integer c=Integer.valueOf(1);   
    Integer d=Integer.valueOf(1);   
      
    //返回的不是同一个对象   
    Integer e=Integer.valueOf(200);   
    Integer f=Integer.valueOf(200);   
      
    System.out.println(a==b);   
    System.out.println(c==d);   
    System.out.println(e==f);
    
      程序运行的结果为:
        false
        true
        false
      结论:在没有对Integer创建有特殊要求时,比如纯数字计算或比较,最好用Integer.valueOf。 

 


   //3个对象直接一共有6种转换方法  
    // 其中 Integer 用了三种 Integer.parseInt()  Integer.intvalue()  Integer.toString()
    //String 用了一种 String.ValueOf()
    //后面会有新特性 

1.为什么要有包装类(或封装类)?


为了使基本数据类型的变量具有类的特征,引入包装类。

2.基本数据类型与对应的包装类:


3.需要掌握的类型间的转换:(基本数据类型、包装类、String)


4.简易速记
基本数据类型<—>包装类:------------JDK 5.0 新特性:自动装箱 与自动拆箱
基本数据类型、包装类—>String:------调用String重载的valueOf(Xxx xxx)
String—>基本数据类型、包装类:------调用包装类的parseXxx(String s)
注意:转换时,可能会报NumberFormatException
 

好的,我可以为您解答这个问题。 在 Java 中,基本数据类型包装类之间可以相互转换。常见的基本数据类型有 int、double、float、boolean、byte、short 和 long,而对应的包装类则是 Integer、Double、Float、Boolean、Byte、Short 和 Long。 将基本数据类型转换包装类可以使用自动装箱的方式,例如: ``` int num = 10; Integer integer = num; // 自动装箱 ``` 将包装类转换基本数据类型可以使用自动拆箱的方式,例如: ``` Integer integer = 10; int num = integer; // 自动拆箱 ``` 另外,在 Java 中,字符串类型 String 也可以转换基本数据类型包装类。例如,将一个字符串转换成整型: ``` String str = "123"; int num = Integer.parseInt(str); ``` 这里使用了 Integer 类的静态方法 parseInt() 将字符串转换为整型。类似的,还有 Double.parseDouble()、Float.parseFloat()、Boolean.parseBoolean()、Byte.parseByte()、Short.parseShort() 和 Long.parseLong() 方法,可以将字符串转换为对应的基本数据类型。 反过来,将一个基本数据类型包装类转换字符串,可以使用 String.valueOf() 方法,例如: ``` int num = 123; String str = String.valueOf(num); ``` 这里使用了 String 类的静态方法 valueOf() 将整型转换字符串。类似的,还有 Double.valueOf()、Float.valueOf()、Boolean.valueOf()、Byte.valueOf()、Short.valueOf() 和 Long.valueOf() 方法,可以将基本数据类型包装类转换为对应的字符串
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kjshuan

点个赞就好啦!!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值