数据类型转换

数据有八大类,有的可进行数据类型的转换,下面是一些基础代码如何进行数据类型的转换

public class lpk03 {
    public static void main(String[] args) 
        //整形拓展  进制:二进制0b  十进制  八进制0  十六进制0x
        int i1 = 10;
        int i2 = 010;   //八进制0
        int i3 = 0x10;    //十六进制0x

        System.out.println(i1);
        System.out.println(i2);
        System.out.println(i3);
        System.out.println("==============================================");


        //============================================================
        //怎么表示银行业务 钱!
        //BigDecimal    数学工具类
        //============================================================
        //float 是有限的    接近但不等于
        //少用浮点数进行比较
        //double
        //最好完全使用浮点数进行比较!
        //最好完全使用浮点数进行比较!
        //最好完全使用浮点数进行比较!
        float d = 0.1f;
        double f = 1.0/10;
        System.out.println(d);
        System.out.println(f);
        System.out.println(d==f);
		//这里不相等!!!
        float x = 12312341414412f;
        float x1 = x + 1;
        System.out.println(x==x1);
        //这里反而相等!!!
        System.out.println("==============================================");
        //============================================================
        //字符类拓展
        //============================================================
        char c1 = 'a';
        char c2 = '中';
        System.out.println(c1);
        System.out.println(c2);
        System.out.println((int)(c1));  //强制类型转换
        System.out.println((int)(c2));  //强制类型转换
        //所有的字符本质还是数字
        //编吗 unicode  2字节  65536个字符

        //转义字符
        // \t  制表符
        // \n  换行
        System.out.println("==============================================");
        String sa = new String( "hello world");
        String sb = new String( "hello world");
        System.out.println(sa==sb);
		//你会发现输出结果为false
        String sc = "hello world";
        String sd = "hello world";
        System.out.println(sc==sd);
		//这里的输出结果为true
        //对象 从内存分析

        //布尔值拓展
        boolean flag = true;
        if (flag==true){ }   //新手程序员
        if (flag){ }
        //less is more  代码精简易读


    }
}

输出结果

10
8
16
============================================
0.1
0.1
false
true
============================================
a
中
97
20013
============================================
false
true

第二节课的学习

public class lpk04 {
    public static void main(String[] args) {
        int i = 128;
        byte j = (byte)i;   //内存溢出

        //类型转换  (类型)变量名   高->低
        //强制类型转换

        double k = i;
        //低->高  自动转换

        //低-------------------------------------->高
        //byte,short,char->int->long->float->double
        System.out.println(i);  //128
        System.out.println(j);  //-128
        System.out.println(k);  //128.0

        /*
        注意点:
        1,不能对布尔型进行转换
        2,不能把对象转换为不相干的类型(例如把人转换成猪不可以,把男人转换成女人可以)
        3,把高容量转换为低容量时,强制转换
        4,转换时可能存在移除,或者精度问题
        */

        System.out.println("=======================");
        System.out.println((int)24.11);   //24
        System.out.println((int)-21.689f);   //-21

        System.out.println("=======================");
        char a = 'a';
        int b = a+1;
        System.out.println(b);
        System.out.println((char) b);

        System.out.println("=======================");
        //操作数比较大时,注意溢出问题
        //JDK7新特性,数字之间可以用下划线_不会影响
        int money = 10_0000_0000;
        System.out.println(money);
        int year = 20;
        int total1 = year*money;
        long total2 = year*money;
        long total3 = money*(long)year;
        System.out.println(total1);     //计算式已经溢出了
        System.out.println(total2);     //默认是int,计算之前已经存在问题
        System.out.println(total3);     //先把一个数转换为long
    }
}

输出结果

128
-128
128.0
=======================
24
-21
=======================
98
b
=======================
1000000000
-1474836480
-1474836480
20000000000
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值