Java里进制转换

进制转换

整数扩展

  • 代码表示

    public class Demo3 {
        public static void main(String[] args) {
            //整数扩展
            //进制表示是数字0不是字符o
            //进制 十进制 二进制0b 八进制0 十六进制0x
            int s1 = 10;
            int s2 = 0b10;
            int s3 = 010;
            int s4 = 0x10;//十六进制表示:0~9 A~F
            System.out.println(s1);
            System.out.println(s2);
            System.out.println(s3);
            System.out.println(s4);
        }
    }
    
    

浮点数扩展

  • 银行业务事例
public class Demo4 {
    public static void main(String[] args) {
        //浮点数扩展   银行业务
        float s1 = 0.1f;
        double s2 = 1.0/10;
        System.out.println(s1==s2);
        System.out.println(s1);
        System.out.println(s2);
    }
}
结果:
false
0.1
0.1

说明我们不能使用float与double去处理这类问题!!

  • 浮点数不能比较大小,不准确
public class Demo5 {
    public static void main(String[] args) {
        //浮点数比较
        //为什么不准确
        //表示有限 结果离散 舍入误差 大约值 接近但不等于 不能表示除不尽的数(无理数、分数)
        //最好避免使用浮点数进行比较
        float s1 = 123356655444f;
        float s2 = s1 + 1;
        System.out.println(s1==s2);
    }
}
结果为:
  true

  • 解决方法
使用
  BigDecimal   数学工具类

字符扩展

  • 代码表示
public class Demo6 {
    public static void main(String[] args) {
        //字符扩展
        char c1 = '中';
        char c2 = 'a';
        System.out.println(c1);
        System.out.println((int) c1);
        System.out.println(c2);
        System.out.println((int) c2);
    }
}
结果为:
  中
  20013   //Unicode编码,中在Unicode代表的数字
  a
  97		//Unicode编码,a在Unicode代表的数字
  • 所有的字符本质还是数字!!
  • 强制转换的编码方式为Unicode,每一个字符对应Unicode表中的一个数字,Unicode的大小为2个字节,也就是2的16次方,65536
  • Unicode的编码格式为 U0000~UFFFF
char c3 = '\u0061';  //  \u为转义字符
System.out.println(c3);
结果为:
  a
  • 早期的Excel表,支持最大的输入位数为2的16次方,65536
  • 转义字符
    • \t 制表符
    • \n 换行

boolean扩展

  • 扩充,//两者表示相同 flag ==true与flag意思相同
public class Demo8 {
    public static void main(String[] args) {
        boolean flag = true;
        //两者表示相同 flag ==true与flag意思相同
        if (flag==true){}
        if (flag){}//省略了==true
      //代码要精简易读
    }
}

引用赋值与直接赋值

  • 代码展示

    public class Demo7 {
        public static void main(String[] args) {
            String s1 = new String("hello");
            String s2 = new String("hello");
            System.out.println(s1==s2);//比较s1与s2引用地址值
            String s3 = "hello";
            String s4 = "hello";
            System.out.println(s3==s4);
        }
    }
    结果为:
      false
      true
    

s1与s2是引用对象,他们的引用地址不同,s3与s4,直接赋值,字符串一样就是一样为true!!

单词

  • original 原来的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值