java中基本数据类型转化以及字符串之间的转化

4.各种数据类型之间的转换

先上链接为敬:
https://blog.csdn.net/tantion/article/details/82626006
https://blog.csdn.net/wang1253/article/details/80752484
https://www.jb51.net/article/127714.htm
https://www.imooc.com/article/43299
https://www.cnblogs.com/yongh/p/9688259.html


java中存在八种基本数据类型

  • byte:8位,最大存储数据量是255,存放的数据范围是-128~127之间。
  • short:16位,最大数据存储量是65536,数据范围是-32768~32767之间。
  • int:32位,最大数据存储容量是2的32次方减1,数据范围是负的2的31次方到正的2的31次方减1。
  • long:64位,最大数据存储容量是2的64次方减1,数据范围为负的2的63次方到正的2的63次方减1。
  • float:32位,数据范围在3.4e-45~1.4e38,直接赋值时必须在数字后加上f或F。
  • double:64位,数据范围在4.9e-324~1.8e308,赋值时可以加d或D也可以不加。
    由于小数常量的默认类型是double型,所以float类型的后面一定要加f(F)。
  • boolean:只有true和false两个取值。
  • char:16位,存储Unicode码,用单引号赋值。
    注意:
    String类并不是基本数据类,而是一个类(class),是C++、java等编程语言中的字符串。

抽象类Number是BigDecimal、BigInteger、Byte、Double、Float、Integer、Long、Short类的超类

JDk1.6-API

JDk1.6-API


例:Byte 类将基本类型 byte 的值包装在一个对象中。一个 Byte 类型的对象只包含一个类型为 byte 的字段。 此外,该类还为 byte 和 String 的相互转换提供了几种方法,并提供了处理 byte 时非常有用的其他一些常量和方法。当然其他类型也类似。

Boolean类将基本类型为boolean的值包装在一个对象中。
以byte转其他类型为例(包装类过渡类型转化,其他几种类似):

public class gao {    
    static byte     byt=0x01;
    static Byte     Byt=new Byte(byt);//Byte、Double、Float、Integer、Long、Short
    static byte     b=Byt.byteValue();
    static short     s=Byt.shortValue();
    static int         i=Byt.intValue();
    static long     l=Byt.longValue();
    static float     f=Byt.floatValue();
    static double     d=Byt.doubleValue();    
    public static void main(String args[]) {
        System.out.println(byt);
        System.out.println(b);
        System.out.println(s);
        System.out.println(i);
        System.out.println(l);
        System.out.println(f);
        System.out.println(d);      
    }
}

结果:

1
1
1
1
1
1.0
1.0

char与其他类型之间的转化:

public class gao {
    public static void main(String args[]) {
        int a=97;
        char c=(char)a; //整形->char加(char)
        int b=c;        //char->整形可以加(int)也可不加
        System.out.println(c);
        System.out.println(b);
    }
}

结果:

a
97

整形、整形数组、字符串、字符串数组、字符数组之间的转化:

  1. 整形-字符串
  2. 字符串-整形
  3. 字符串-字符串数组
  4. 字符串数组-字符串
  5. 字符串-字符数组
  6. 字符数组-字符串
  7. 整形数组-字符串
  8. 字符串-整形数组

整形数组转化为字符数组(整形数组先转为字符串、字符串转为字符数组)
字符数组转化为整形数组(字符数组转为字符串、字符串转化为整形数组)

1.其他基本类型向字符串转化:

  • 将基本数据类型与空字符串(" ")连接(+)即可获得其所对应的字符串
  • 调用String类中的valueOf()方法返回相应的字符串
  • 使用包装类的toString()方法
public class gao {
    public static void main(String args[]) {
        int i=123;
        String str1=i+"";
        String str2=String.valueOf(i);
        String str3=Integer.toString(i);
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);       
    }
}

结果:

123
123
123

2.字符串向其他基本类型转化:

  • 调用基本数据类型对应的包装类中的方法ParseXXX(String)
  • 对应包装类型的valueOf(String)
public class gao {
    public static void main(String args[]) {
        String str="123";
        int a=Integer.parseInt(str);
        int b=Integer.valueOf(str);
        System.out.println(a);
        System.out.println(b);
    }
}

结果:

123
123

如果需要将一个字符串转换成为一个数值类型数据,那么需要注意的是,字符串中得有符合数值类型的数字,否则将无法转换。
如:

public class gao {
    public static void main(String args[]) {    
            String str="123abc";
            int a=Integer.parseInt(str);            
            System.out.println(a);      
    }
}//则会报错

 


5.字符串转字符数组
3.字符串转字符串数组

public class gao {
    public static void main(String args[]) {
        String str = "abcbcddef";
        char[] ch=str.toCharArray();
        for(int i=0;i<ch.length;i++) {
            System.out.println(ch[i]);
        }       
        String[] arr=str.split("");
        for(int i=0;i<arr.length;i++) {
            System.out.println(arr[i]);
        }
    }
}

//当然 也可以用循环依次放入,
//取得字符串的某个元素用 String.charAt(i); 返回char

char[] cha= new char[str.length()];
for(int i =0;i<str.length();i++) {
     cha[i] = str.charAt(i);
}

4.字符串数组转字符串

public class gao {
    public static void main(String args[]) {
        String[] str={"abc","bcd","def"};
        StringBuffer SB=new StringBuffer();
        for(int i=0;i<str.length;i++){
            SB.append(str[i]);
        }
        String s=SB.toString();
        System.out.println(s);
    }
}//输出abcbcddef

6.字符数组转字符串

(1).
public class gao {
    public static void main(String args[]) {
        char[] data={'a','b','c'};
        String s=new String(data);
    }
}//输出abc
(2).
public class gao {
    public static void main(String args[]) {
        char[] data={'a','b','c'};
        String string =String.copyValueOf(data);
}
}//输出abc

7.整形数组转化为字符串

public class gao {
    public static void main(String args[]) {
        int[] a= {1,2,3,4};
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<a.length;i++) {
            sb.append(a[i]);
        }
        System.out.println(sb);
    }
}//输出1234

8.字符串转化为整形数组(先将字符串转为字符串数组)

public class gao {
    public static void main(String args[]) {
        String str="100200300";
        String[] temp=str.split("");
        int[] result=new int[temp.length];
        for(int i=0;i<temp.length;i++) {
            result[i]=Integer.parseInt(temp[i]);
        }
        for(int i=0;i<result.length;i++) {
            System.out.print(result[i]);
        }
    }
}//输出100200300
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

高二的笔记

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值