java之Number与Math及Random类

目录

包装类

基本类型与包装类型对应关系

Number

概述

 方法(子类创建对象时使用)

Integer

属性

构造器

包装类的特性

自动装箱

自动拆箱

方法

compareTo()

equal() 

intvalue()

parseInt()

BigDecimal

推荐创建对象方式

方法

关于除法舍入方式

参数

Math类

前言:

常用属性

常用方法

Random类

构造器

空参构造器创建对象

带参构造器创建对象

包装类

含义:将基本数据类型进行了封装,产生一个新的类,该类提供很多方法与功能,这就是包装类

引入包装类的原因:java是面向对象的语言,最擅长操作各种各样的类,通过包装类可以操作各种各样的属性、方法、构造器以实现更高级的功能 

基本类型与包装类型对应关系

基本数据类型对应的包装类继承关系
byteByte—>Number—>Object
shortShort—>Number—>Object
intInteger—>Number—>Object
longLong—>Number—>Object
floatFloat—>Number—>Object
doubleDouble—>Number—>Object
charCharacterObject
booleanBooleanObject

注意:

  • 包装类为引用数据类型,其对基本数据类型进行了封装
  • 包装类所属的包为java.lang,意味着我们使用的时候无需导包

Number

概述

注意:number为包装类的抽象父类,不可直接创建对象

 方法(子类创建对象时使用)

Integer

注意:Integer类被final修饰意味着该类不能有子类,不能被继承

属性

  • MAX_VALUE:值为2^31-1,他表示int类型能够表示的最大值
  • MIN_VALUE:值为-2^31,他表示int类型能够表示的最小值
        System.out.println(Integer.MAX_VALUE);//2147483647
        System.out.println(Integer.MIN_VALUE);//-2147483648
        //物极必反,最大值+1变成最小值,最小值-1变成最大值
        System.out.println(Integer.MAX_VALUE+1);//-2147483647
        System.out.println(Integer.MIN_VALUE-1);//2147483648

构造器

注意:

  • Integer没有空参构造器
  • Integer重写了toString()方法,直接返回了对应的value值

        //int类型作为构造器的参数
        Integer integer = new Integer(12);
        System.out.println(integer.toString());//12
        //String类型作为构造器的参数
        Integer integer1 = new Integer("12");//将string字符串转为int类型的数
        System.out.println(integer1);//12

包装类的特性

特性:自动装箱;自动拆箱

自动装箱与自动拆箱原理:将基本数据类型和包装类型进行了快速的类型转换

自动装箱

        //自动装箱
        Integer i=12;
        System.out.println(i);//12
        //自动装箱底层原理
        Integer i2=Integer.valueOf(13);
        System.out.println(i2);

自动拆箱

        //自动拆箱
        Integer integer = new Integer(23);
        int num=integer;
        System.out.println(num);//23
        //自动拆箱底层原理
        Integer integer1=new Integer(33);
        int num1=integer1.intValue();
        System.out.println(num1);

方法

compareTo()

语法:compareTo(Integer i)

作用:在数值上比较两个Integer的数,如果数值大于i那么返回1,如果数值小于i则返回-1,等于i则返回0

返回值:int

        Integer integer = new Integer(23);
        Integer integer1 = new Integer(25);
        int i = integer.compareTo(integer1);
        System.out.println(i);//-1

equal() 

语法:equal(Object obj)

作用:比较两个值是否相等

返回值:boolean

        Integer integer = new Integer(23);
        Integer integer1 = new Integer(23);
        System.out.println(integer.equals(integer1));//true——比较数值是否相等
        System.out.println(integer==integer1);//false——比较地址值是否相等

自动装箱下的==比较

        Integer i1=12;
        Integer i2=12;
        System.out.println(i1.equals(i2));//true
        System.out.println(i1==i2);//true

        Integer n1=130;
        Integer n2=130;
        System.out.println(n1.equals(n2));//true
        System.out.println(n1==n2);//false

注意:如果自动装箱的值在128——127之间那么比较的是具体数值的大小,否则比较的是地址值的大小(底层缓存数组);在Double类中没有此现象

intvalue()

语法:intValue()

作用:以int类型返回Integer的值

返回值:int

        Integer i1=13;
        int i = i1.intValue();
        System.out.println(i);//13

parseInt()

语法:Integer.parseInt(String s)

作用:将字符串参数作为有符号的十进制整数进行解析

返回值:int

        String s="128";
        int i = Integer.parseInt(s);
        System.out.println(i);

BigDecimal

作用:用于解决浮点数运算不精确问题

推荐创建对象方式

语法:BigDecimal(String s)

作用:将String类型字符串的形式转换为BigDecimal

返回值:BigDecimal类型的小数

方法

做加法运算:add(BigDecimal bd) 
做减法运算:subtract(BigDecimal bd) 
做乘法运算:multiply(BigDecimal bd) 

        BigDecimal bd1 = new BigDecimal("1.2");
        BigDecimal bd2 = new BigDecimal("2.4");
        //加法
        BigDecimal add = bd1.add(bd2);
        //减法
        BigDecimal sub = bd1.subtract(bd2);
        //乘法
        BigDecimal mul = bd1.multiply(bd2);
        System.out.println("相加得:"+add+"\n相减得:"+sub+"\n相乘得:"+mul);

除法运算: 

  • Divide(BigDecimal bd)
  • Divide(BigDecimal bd,保留小数位数,舍入方式)

注意:Divide(BigDecimal bd)做除法运算,除不尽时会抛异常

        BigDecimal bd1 = new BigDecimal("6.38");
        BigDecimal bd2 = new BigDecimal("2.1");
        BigDecimal divide = bd1.divide(bd2,4, BigDecimal.ROUND_HALF_UP);
        System.out.println(divide);

关于除法舍入方式

使用方式:BigDecimal.参数

参数

  • ROUND_HALF_UP 四舍五入
  • ROUND_HALF_DOWN 五舍六入
  • ROUND_HALF_EVEN 公平舍入(在5和6之间,靠近5就舍弃成5,靠近6就进位成6,如果是5.5,就变成6)
  • ROUND_UP 直接进位
  • ROUND_DOWN 直接舍弃
  • ROUND_CEILING(天花板) 向上取整
  • ROUND_FLOOR(地板) 向下取整

Math类

前言:

  • 所在包为java.lang意味着我们可以直接使用而不用导包
  • Math类被final修饰,该类不能被继承
  • Math构造器私有化,不能创建Math类的对象
  • Math内部所有的属性和方法都被static修饰,可以通过类名.属性直接调用

常用属性

语法:Math.PI

作用:该值为圆周率的值

常用方法

生成[0.0,1.0)之间的随机数:Math.Random()——底层调用的是Random类

取绝对值:Math.abs(整形或浮点类型的数)

向上取整:Math.ceil(浮点数)

向下取整:Math.floor(浮点数)

四舍五入:Math.round(浮点数)

求最大值:Math.max(值1,值2)

求最小值:Math.min(值1,值2)

        System.out.println("圆周率为"+Math.PI);
        System.out.println("0-1的随机数为"+Math.random());
        System.out.println("绝对值"+Math.abs(-66));
        System.out.println("向上取整"+Math.ceil(8.2));
        System.out.println("向下取整"+Math.floor(8.9));
        System.out.println("四舍五入"+Math.round(8.9));
        System.out.println("最大值"+Math.max(4, 4.6));
        System.out.println("最小值"+Math.min(4, 4.6));

Random类

构造器

空参构造器创建对象

        //利用空参构造器创建对象
        Random random = new Random();
        //获取[0,10)之间的随机整数数
        int i = random.nextInt(10);
        System.out.println(i);
        //获取[0.0,1.0)之间的随机小数
        double v = random.nextDouble();
        System.out.println(v);

注意:无参构造器表面在调用无参构造函数 ,实际底层还是调用了带参构造方法

带参构造器创建对象

        //内部传一个不断变化的long类型数,得到随机数生成器
        Random random = new Random(System.currentTimeMillis());
        //利用随机生成器获取int类型随机数
        int i = random.nextInt();
        System.out.println(i);
        //获取[0,10)之间的随机整数
        int i1 = random.nextInt(10);
        System.out.println(i1);
        //获取[0.0,1.0)之间的随机小数
        double v = random.nextDouble();
        System.out.println(v);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值