• 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类型能够表示的最小值
    1. System.out.println(Integer.MAX_VALUE);//2147483647
    2. System.out.println(Integer.MIN_VALUE);//-2147483648
    3. //物极必反,最大值+1变成最小值,最小值-1变成最大值
    4. System.out.println(Integer.MAX_VALUE+1);//-2147483647
    5. System.out.println(Integer.MIN_VALUE-1);//2147483648

    构造器

    注意:

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

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

    包装类的特性

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

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

    自动装箱

    1. //自动装箱
    2. Integer i=12;
    3. System.out.println(i);//12
    4. //自动装箱底层原理
    5. Integer i2=Integer.valueOf(13);
    6. System.out.println(i2);

    自动拆箱

    1. //自动拆箱
    2. Integer integer = new Integer(23);
    3. int num=integer;
    4. System.out.println(num);//23
    5. //自动拆箱底层原理
    6. Integer integer1=new Integer(33);
    7. int num1=integer1.intValue();
    8. System.out.println(num1);

    方法

    compareTo()

    语法:compareTo(Integer i)

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

    返回值:int

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

    equal() 

    语法:equal(Object obj)

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

    返回值:boolean

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

    自动装箱下的==比较

    1. Integer i1=12;
    2. Integer i2=12;
    3. System.out.println(i1.equals(i2));//true
    4. System.out.println(i1==i2);//true
    5. Integer n1=130;
    6. Integer n2=130;
    7. System.out.println(n1.equals(n2));//true
    8. System.out.println(n1==n2);//false

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

    intvalue()

    语法:intValue()

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

    返回值:int

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

    parseInt()

    语法:Integer.parseInt(String s)

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

    返回值:int

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

    BigDecimal

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

    推荐创建对象方式

    语法:BigDecimal(String s)

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

    返回值:BigDecimal类型的小数

    方法

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

    1. BigDecimal bd1 = new BigDecimal("1.2");
    2. BigDecimal bd2 = new BigDecimal("2.4");
    3. //加法
    4. BigDecimal add = bd1.add(bd2);
    5. //减法
    6. BigDecimal sub = bd1.subtract(bd2);
    7. //乘法
    8. BigDecimal mul = bd1.multiply(bd2);
    9. System.out.println("相加得:"+add+"\n相减得:"+sub+"\n相乘得:"+mul);

    除法运算: 

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

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

    1. BigDecimal bd1 = new BigDecimal("6.38");
    2. BigDecimal bd2 = new BigDecimal("2.1");
    3. BigDecimal divide = bd1.divide(bd2,4, BigDecimal.ROUND_HALF_UP);
    4. 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)

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

    Random类

    构造器

    空参构造器创建对象

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

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

    带参构造器创建对象

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

  • 相关阅读:
    【MySQL】数据库的操作
    正则表达式符号
    ubuntu系统开机黑屏(只显示logo、左上角光标闪烁)问题
    为什么 Spring的构造器注入不需要 @Autowired 注解
    ChatRule:基于知识图推理的大语言模型逻辑规则挖掘11.10
    优盘格式化了怎么恢复里面的数据?
    2.2 Linux系统的目录结构与文件类型
    英语口语常用1368词汇
    无人车开源软件架构
    目标检测:二维码检测方案
  • 原文地址:https://blog.csdn.net/m0_60027772/article/details/126081300