0
点赞
收藏
分享

微信扫一扫

java 中字符串 包装类 基本类型的相互转换代码实现

四月天2021 2022-04-23 阅读 58

文章目录

回顾

基本数据类型和包装类的相互转化即自动装箱和自动拆箱:
在这里插入图片描述

参考文档

1.基本类型对应的包装类

byte -->Byte
short --> Short
int -->Integer
long -->Long
float -->Float
double -->Double

char -->Character
boolean -->Boolean

1.1 基本数据类型与包装类之间的转换

装箱:将基本数据类型转换成对应的包装类

①通过对应包装类的构造器装箱

Byte构造器

 public Byte(byte value) {
        this.value = value;
    }

Short构造器

 public Short(short value) {
        this.value = value;
    }

Integer构造器

 public Integer(int value) {
	this.value = value;
    }

Long构造器

public Long(long value) {
        this.value = value;
    }

Float

 public Float(float value) {
        this.value = value;
    }

Double构造器

 public Double(double value) {
        this.value = value;
    }

包装类底层代码实现逻辑分析:

//1.new 创建Integer对象进行实例化

Integer i=new Integer(10);//实例化Integer有参构造传入int数值(value==10)

//2.Integer有参构造接收传入的int值value(等于传入的10)

  public Integer(int value) {
        this.value = value;
    

//3.利用this.value = value代码将传入的参数赋值给常量:

value等于实例化传入的参数(此处等于数值10)

this.value调用的变量代码如下:

 private final int value;

在这里插入图片描述

根据以上代码实现原理分析可知:Integer构造器底层就是final修饰的int 类型

包装类Byte Short Integer Long Float Double 之间的关系:

1、基本数据类型中byte— short---- int–long—float --double存在大小关系之间需要转化:
自动转换:小–大

//部分案例
int a=10;
long b=a;

byte c=20;
int =c;

强制转换:大–小

//部分案例
 long a1=25;
 int a2=(int) a1;
 double a3=455.88d;
 int a4= (int) a3;

2、String 与Byte Short Integer Long Float Double(包装类)之间的转换:valueOf()和parse##()方法

因为包装类的底层都是有final修饰基本数据类型,所以包装类之间也存在基本数据类型之间的转化关系

         String str="100";
    
    //string  -byte
        System.out.println(Byte.valueOf(str));
        System.out.println(Byte.parseByte(str));
     //string ---int   
        System.out.println(Integer.valueOf(str));
        System.out.println(Integer.parseInt(str));
   //string ---Short
        System.out.println(Short.valueOf(str));
        System.out.println(Short.parseShort(str));
    //string---long
        Long.parseLong(str);
        Long.valueOf(str)
        String str="100.10d";
        //string---Double
        System.out.println(Double.parseDouble(str));
        System.out.println(Double.valueOf(str));
         String str="100.10f";
         //string----float
        System.out.println(Float.parseFloat(str));
        System.out.println(Float.valueOf(str));

 
  

拆箱:将包装类转化为基本数据类型

②通过对应包装类的静态方法 valueOf()

1、将包装类转换成对应的基本数据类型

//部分案例
      byte b=10;
      System.out.println(Byte.valueOf(b));
      public static Byte valueOf(byte b){}//底层代码
      
      long l=30l;
      System.out.println(Long.valueOf(100l));
      public static Long valueOf(long l) {}//底层代码
      
      int a=40;
      System.out.println(Integer.valueOf(10));
      public static Integer valueOf(int i){}//底层代码

      
   

    

①基本数据类型转化为 string
valueOf()方法

        int a=10;
        System.out.println(String.valueOf(a));
        long b=20;
        System.out.println(String.valueOf(b));
        float c=30f;
        System.out.println(String.valueOf(c));

总结:1.基本数据类型之间的转换;2.基本数据类型与包装类型之间的关系转化;3基本数据类型与string的关系转化;4.包装类与string类的关系转化

参考文档

参考文档

举报

相关推荐

0 条评论