首页 > 技术文章 > Java基础--关键字、数据类型、类型转换

sinlearn 2020-07-22 19:55 原文

关键字(35个)

image.png
查看Java关键字

数据类型

Java是强类型语言:要求变量的使用严格符合规定,所有变量都必须先定义后才能使用
弱类型语言:JavaScript
Java的数据类型分为两大类:基本数据类型、引用类型

public class Demo02 {
    public static void main(String[] args) {
        // 8大基本数据类型
        byte a = 20;
        short b = 1;
        int  c = 2;
        long d = 1000L;
        float e = 3.1f;
        double f = 4.20;
        char ch = 'a';
        boolean flag = true;
                
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
        System.out.println(e);
        System.out.println(f);
        System.out.println(ch);
        System.out.println(flag);
    }
}
public class Demo03 {
    public static void main(String[] args) {
        //=============================================
        // 整数拓展: 进制 二进制 八进制 十进制 十六进制
        //=============================================
        int i = 10;
        int i2 = 010;
        int i3 = 0x12;

        System.out.println(i);
        System.out.println(i2);
        System.out.println(i3);
        //=============================================
        // 浮点拓展 银行算钱
        // BigDecimal 数学工具类
        //=============================================
        // float  有限 离散  舍入误差 接近但不等于
        float f = 0.1f;
        double d = 1.0/10;

        System.out.println(f==d); // false
        float d1 = 23232434344354545f;
        float d2 = d1 + 1;
        System.out.println(d1==d2); // true
        //=============================================
        // 字符拓展
        // 所有字符的本质都是数字
        //编码 Unicode 2字节  0-65536 Excel 2的16次方=65536
        // A 65 a 97
        // U0000 - UFFFF
        // 转义字符
        // \t \n \b
        //=============================================
        char c1 = 'a';
        char c2 = '中';
        char c3 = '\u0061';
        System.out.println(c1);
        System.out.println((int)c1);
        System.out.println(c2);
        System.out.println((int)c2);
        System.out.println(c3);
        
        String sa = new String("hello world");
        String sb = new String("hello world");
        // 涉及到对象的内存分析
        String sc = "hello world";
        String sd = "hello world";
        System.out.println(" // ========================");
        System.out.println(sa==sb); // 不等
        System.out.println(sc==sd); // 等

        //=============================================
        // 布尔拓展
        //=============================================
        boolean flag = true;
        if(flag==true){}    // 新手
        if (flag) {}       //老手
        
    }
}

类型转换

强制转换 : 高到低
自动转换: 低到高
image.png
 强制转换需要注意点:
           1).不能对布尔类型进行转换。
    2).把一个浮点数强制转换为整数时,java会直接截断浮点数的小数部分。
    3).把一个超出数据范围的数值赋给数据类型是,会出现数据溢出的情况,造成数据的缺失。

public class Demo06 {
    public static void main(String[] args) {
        //操作比较大的数的时候,注意溢出问题
        // JDK7的新特性 数字之间可以用下划线分割
        int money =  10_0000_0000;
        int years = 20;
        int total = money * years;  // 1474836480, 计算的时候溢出了
        long total2 = money * years;  // 默认是int, 转换之前就存在溢出了
        long total3 = money * ((long) years);
        System.out.println(total3);
    }
}

推荐阅读