java基础04-数据类型扩展

整数拓展

public static void main(String[] args) {

    // 整数拓展:进制
    // 二进制0b开头 八进制0开头 十进制 十六进制0x
    int i1 = 10;
    int i2 = 0b10;
    int i3 = 010;
    int i4 = 0x10;

    System.out.println(i1); // 10
    System.out.println(i2); // 2
    System.out.println(i3); // 9
    System.out.println(i4); // 16
}

浮点数扩展

// ======================================================================================
// 浮点数拓展
// float 特点:位数有限、离散型、有舍入误差、数值是大约的、接近但不等于。
// double
// 所以最好不要用浮点数进行比较。
// 如果是针对精确数值和银行的钱标识的话 我们需要用到引用类型:BigDecimal类。

// 例子
float f = 0.1f;
double d = 1.0 / 10;
System.out.println(f); // 0.1
System.out.println(d); // 0.1
System.out.println(f == d); // false

float f1 = 215123451321f;
float f2 = f1 + 1;
System.out.println(f1 == f2); // true

// 所以得出上述结果

字符扩展

// ======================================================================================
// 字符拓展
char c1 = 'a';
char c2 = 'A';
char c3 = '中';
System.out.println((int) c1); // 97
System.out.println((int) c2); // 65
System.out.println((int) c3); // 20013

// 结论
// 所有的字符本质还是数字
// 存在Unicode编码表中 数字范围:0~65536
// 官方书写方式 u0000~uFFFF
System.out.println('u0065');

// 转义字符
// 	 制表符
// 
 换行
// ......
System.out.println("hello	word");
System.out.println("hello
word");
欢迎一起来学习和指导,谢谢关注!
原文地址:https://www.cnblogs.com/mask-xiexie/p/14563252.html