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');

// 转义字符
// \t 制表符
// \n 换行
// ......
System.out.println("hello\tword");
System.out.println("hello\nword");
posted @   xiexie0812  阅读(50)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示