Java中常见的类型转换问题

Java中常见的类型转换问题

初始化

        int demoInt = 10;
        long demoLong = 1111111111222222222l;
        short demoShort = 11;
        float demoFloat = 2123.233f;
        double demoDouble = 123.123213;

将XXX类型转换为String类型:

方法一:

        String intToStr = Integer.toString(demoInt);
        String longToStr = Long.toString(demoLong);
        String shortToStr = Short.toString(demoShort);
        String floatToStr = Float.toString(demoFloat);
        String doubleToStr = Double.toString(demoDouble);

方法二:
相比方法一少了short类型。

        String intToStrT = String.valueOf(demoInt);
        String longToStrT = String.valueOf(demoLong);
        String floatToStrT = String.valueOf(demoFloat);
        String doubleToStrT = String.valueOf(demoDouble);

方法三:
直接用“”+相关类型,转换为String类型。

将String类型转换为XXX类型

方法一:

        int strToInt = Integer.parseInt(intToStr);
        long strToLong = Long.parseLong(longToStr);
        short strToShort = Short.parseShort(shortToStr);
        float strToFloat = Float.parseFloat(floatToStr);
        double strToDouble = Double.parseDouble(doubleToStr);

方法二:

        int strToIntT = Integer.valueOf(intToStr);
        long strToLongT = Long.valueOf(longToStr);
        short strToShortT = Short.valueOf(shortToStr);
        float strToFloatT = Float.valueOf(floatToStr);
        double strToDoubleT = Double.valueOf(doubleToStr);

将String类型转换为Char类型、将字符Char转换为Int类型

String类型转换为Char类型:

  • 第一种,循环后charAt(i);
    注意:charAt(i)得到的是字符串所对应的字符,为ASCII值,不能直接转换为int型。
  • 第二种:char[] temp = str.toCharArray();
    注意:char[]里的内容是每位字符的ASCII值。

将字符Char类型转换为Int类型:

  • 第一种:利用当前字符减去字符0(‘0’)。
  • 第二种:将字符转为字符串,再转为int型。
        String strDemo = "12348765";
        int sum_one = 0;
        int sum_two = 0;

        System.out.println("第一种方法:");
        for(int i=0; i<strDemo.length();i++){
            //字符串String转字符Char第一种方法
            char char_one = strDemo.charAt(i);
            if (i!=strDemo.length()-1){
                System.out.print(char_one+"|***|");
            }else {
                System.out.println(char_one);
            }
            //将字符char转换为数字int的第一种方法
            int int_one = strDemo.charAt(i) - '0';
            sum_one += int_one;
        }
        System.out.println("第一种:"+sum_one);

        System.out.println("第二种方法:");
        char[] char_two = strDemo.toCharArray();
        for (int i=0; i< char_two.length; i++){
            //字符串String转字符Char第二种方法
            if (i!=strDemo.length()-1){
                System.out.print(char_two[i]+"|***|");
            }else {
                System.out.println(char_two[i]);
            }
            int int_two = Integer.parseInt(String.valueOf(char_two[i]));

            sum_two += int_two;
        }
        System.out.println("第二种:"+sum_two);

基本数据类型

整型数据类型有:byte(8bits)、short(16bits)、int(32bits)、long(64bits)
浮点型数据类型有:单精度(32bits float)、双精度(64bits double)
boolean类型变量的取值有:ture、false
char数据类型有:unicode字符,16位
对应的类类型:Integer、Float、Boolean、Character、Double、Short、Byte、Long

其他

  • xxxvalue()方法用于将Number对象转换为XXX数据类型的值。
  • 转换原则:从低精度向高精度转换。
    byte 、short、int、long、float、double、char
    注:两个char型运算时,自动转换为int型;当char与别的类型运算时,也会先自动转换为int型的,再做其它类型的自动转换。

全文代码:

//用于试验各种字符串转换
public class toConversion {
    public static void main(String[] args){

        System.out.println("--------------将XXX类型转换为String类型-------------------------");
        int demoInt = 10;
        long demoLong = 1111111111222222222l;
        short demoShort = 11;
        float demoFloat = 2123.233f;
        double demoDouble = 123.123213;

        String intToStr = Integer.toString(demoInt);
        String longToStr = Long.toString(demoLong);
        String shortToStr = Short.toString(demoShort);
        String floatToStr = Float.toString(demoFloat);
        String doubleToStr = Double.toString(demoDouble);
        System.out.println("int To string:" + intToStr);
        System.out.println("long To string:" + longToStr);
        System.out.println("short To string:" + shortToStr);
        System.out.println("float To string:" + floatToStr);
        System.out.println("double To string:" + doubleToStr);

        String intToStrT = String.valueOf(demoInt);
        String longToStrT = String.valueOf(demoLong);
        String floatToStrT = String.valueOf(demoFloat);
        String doubleToStrT = String.valueOf(demoDouble);
        System.out.println("int To string two:" + intToStrT);
        System.out.println("long To string two:" + longToStrT);
        System.out.println("float To string two:" + floatToStrT);
        System.out.println("double To string two:" + doubleToStrT);

        System.out.println("--------------将String类型转换为XXX类型-------------------------");
        int strToInt = Integer.parseInt(intToStr);
        long strToLong = Long.parseLong(longToStr);
        short strToShort = Short.parseShort(shortToStr);
        float strToFloat = Float.parseFloat(floatToStr);
        double strToDouble = Double.parseDouble(doubleToStr);
        System.out.println("String To int:" + strToInt);
        System.out.println("String To long:" + strToLong);
        System.out.println("String To short:" + strToShort);
        System.out.println("String To float:" + strToFloat);
        System.out.println("String To double:" + strToDouble);

        int strToIntT = Integer.valueOf(intToStr);
        long strToLongT = Long.valueOf(longToStr);
        short strToShortT = Short.valueOf(shortToStr);
        float strToFloatT = Float.valueOf(floatToStr);
        double strToDoubleT = Double.valueOf(doubleToStr);

        System.out.println("String To int two:" + strToIntT);
        System.out.println("String To long two:" + strToLongT);
        System.out.println("String To short two:" + strToShortT);
        System.out.println("String To float two:" + strToFloatT);
        System.out.println("String To double two:" + strToDoubleT);

        System.out.println("--------------将String类型转换为Char类型、将字符Char转换为Int类型--------------------");
        String strDemo = "12348765";
        int sum_one = 0;
        int sum_two = 0;

        System.out.println("第一种方法:");
        for(int i=0; i<strDemo.length();i++){
            //字符串String转字符Char第一种方法
            char char_one = strDemo.charAt(i);
            if (i!=strDemo.length()-1){
                System.out.print(char_one+"|***|");
            }else {
                System.out.println(char_one);
            }
            //将字符char转换为数字int的第一种方法
            int int_one = strDemo.charAt(i) - '0';
            sum_one += int_one;
        }
        System.out.println("第一种:"+sum_one);

        System.out.println("第二种方法:");
        char[] char_two = strDemo.toCharArray();
        for (int i=0; i< char_two.length; i++){
            //字符串String转字符Char第二种方法
            if (i!=strDemo.length()-1){
                System.out.print(char_two[i]+"|***|");
            }else {
                System.out.println(char_two[i]);
            }
            int int_two = Integer.parseInt(String.valueOf(char_two[i]));
            sum_two += int_two;
        }
        System.out.println("第二种:"+sum_two);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值