• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
CudGlory
博客园    首页    新随笔    联系   管理    订阅  订阅
基本数据类型的拓展问题
public class Demo03 {
    public static void main(String[] args) {
        //整数拓展:  进制问题   二进制(0b开头)   十进制    八进制(0开头)    十六进制(0x开头)

        int i = 10;
        int i1 = 010;//八进制是以0开头
        int i2 = 0x10;//十六进制是以0x开头     十六进制内   10~16对应的是A~F

        System.out.println(i);
        System.out.println(i1);
        System.out.println(i2);
        System.out.println("=================================================================");
        //=====================================================================
        //浮点数扩展? 银行业务怎么表示?钱
        //BigDecimal 数学工具类
        //=====================================================================
        //float       有限   离散   舍入误差  大约  接近但不等于
        //double
        //最好完全避免使用浮点数进行比较
        //最好完全避免使用浮点数进行比较
        //最好完全避免使用浮点数进行比较

        float f = 0.1f;//0.1
        double d = 1.0/10;//0.1

        System.out.println(f);
        System.out.println(d);
        System.out.println(f==d);

        float d1 = 2131434254617f;
        float d2 = d1 + 1;

        System.out.println(d2==d1);

        //=====================================================================
        //字符拓展?
        //=====================================================================
        char c1 = 'a';
        char c2 = '中';

        System.out.println(c1);
        System.out.println((int)c1);//强制转换
        System.out.println(c2);
        System.out.println((int)c2);//强制转换

        //所有的字符本质还是数字
        //编码    Unicode表:(97=a  65=A)  占2字节   长度:0~65536    Excel   长度:2的16次方=65536

        // U0000  UFFFF
        char c3='\u0061';
        System.out.println(c3);//a

        //转义字符
        //\t  制表符(相当于Tab)
        //\n   换行
        System.out.println("=============================================================");
        System.out.println("Hello\tWorld");
        System.out.println("Hello\nWorld");

        System.out.println("=============================================================");
        String sa = new String("hello world");
        String sb = new String("hello world");
        System.out.println(sa==sb);//false    new是对象 从内存分析

        String sc="hello world";
        String sd="hello world";
        System.out.println(sc==sd);

        //布尔值扩展
        Boolean flag = true;
        if(flag==true){}  //新手
        if(flag){}  //老手
        //Less is More!代码要精简易读

    }
}

避免使用浮点数进行数据比较

因为浮点数具有有限、离散和舍入误差性,使用时自动近似,导致比较结果出现误差

posted on 2022-02-09 16:05  CudGlory  阅读(35)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3