Java_类的介绍_13

Math类

Math用于做数学运算。
Math类中的方法全部是静态方法,直接用类名调用即可
方法:
方法名 说明
public static int abs(int a) 获取参数a的绝对值:
public static double ceil(double a) 向上取整
public static double floor(double a) 向下取整
public static double pow(double a, double b) 获取a的b次幂
public static long round(double a) 四舍五入取整

代码实例:

public class MathDemo {
    public static void main(String[] args) {
        // 1.取绝对值:返回正数。
        System.out.println(Math.abs(10));
        System.out.println(Math.abs(-10.3));
        // 2.向上取整: 5
        System.out.println(Math.ceil(4.00000001)); // 5.0
        // 3.向下取整:4
        System.out.println(Math.floor(4.99999999)); // 4.0
        // 4.求指数次方
        System.out.println(Math.pow(2 , 3)); // 2^3 = 8.0
        // 5.四舍五入 10
        System.out.println(Math.round(4.49999)); // 4
        System.out.println(Math.round(4.500001)); // 5
    }
}

System系统类

System代表当前系统。
静态方法:
1.public static void exit(int status):终止JVM虚拟机,非0是异常终止。
2.public static long currentTimeMillis():获取当前系统此刻时间毫秒值。
3.可以做数组的拷贝。
arraycopy(Object var0, int var1, Object var2, int var3, int var4);
* 参数一:原数组
* 参数二:从原数组的哪个位置开始赋值。
* 参数三:目标数组
* 参数四:赋值到目标数组的哪个位置
* 参数五:赋值几个。

代码实例:

public class _system01 {
    public static void main(String[] args){
        System.out.println("程序开始运行");
        //System.exit(0);  //0 代表正常终止
        //得到当前虚拟机的毫秒值
        long time = System.currentTimeMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        System.out.println(sdf.format(time));
        System.out.println("程序结束运行");
        //可以数组拷贝(了解)
        int[] arr1 = new int[]{10,20,30,40,50,60,70};
        int[] arr2 = new int[6];  //6个0
        /**
         arraycopy(Object src,int  srcPos ,Object dest, int destPos,  int length)
         参数一:原数组
         参数二:从哪个索引位置开始赋值
         参数三:目标数组
         参数四:目标数组的开始索引:
         参数五:复制几个
         */
        System.arraycopy(arr1,2, arr2 , 1 , 3);
        System.out.println(Arrays.toString(arr2));
    }
}

BigDecimal解决精度运算问题

  1. 引入:
    浮点型运算的时候直接+ * / 可能会出现数据失真(精度问题)。
    BigDecimal可以解决浮点型运算数据失真的问题。

  2. BigDicimal类:
    1. 包:java.math.
    2. 创建对象的方式(最好的方式:)
    public static BigDecimal valueOf(double val) :包装浮点数成为大数据对象。
    3. 方法声明
    public BigDecimal add(BigDecimal value) 加法运算
    public BigDecimal subtract(BigDecimal value) 减法运算
    public BigDecimal multiply(BigDecimal value) 乘法运算
    public BigDecimal divide(BigDecimal value) 除法运算
    public double doubleValue():把BigDecimal转换成double类型。

public static void main(String[] args){
        double a = 0.1,b = 0.2;

        BigDecimal a1 = BigDecimal.valueOf(a);
        BigDecimal b1 = BigDecimal.valueOf(b);
        BigDecimal c = a1.add(b1); //加法
        BigDecimal c1 = a1.subtract(b1); //减法
        BigDecimal c2 = a1.multiply(b1); //乘法
        BigDecimal c3 = a1.divide(b1); //除法
        System.out.println(c);
        //BigDecimal 只是解决精度问题的手段,double数据才是目的。最后要变类型
        double r = c1.doubleValue();
    }

包装类

  1. 引入:
    Java认为一切皆对象。引用数据类型就是对象了。
    但是在Java中8基本数据类型不是对象,只是表示一种数据的类型形式,这8种数据类型显得很突兀。
    Java为了一切皆对象的思想统一,把8种基本数据类型转换成对应的类,这个类称为
    基本数据类型的包装类。

    基本数据类型 包装类(引用数据类型)
    byte Byte
    short Short
    int Integer(特殊)
    long Long

      float                     Float
      double                    Double
      char                      Character(特殊)
      boolean                   Boolean
    

自动装箱:可以直接把基本数据类型的值或者变量赋值给包装类。
自动拆箱:可以把包装类的变量直接赋值给基本数据类型。

小结:
自动装箱:可以直接把基本数据类型的值或者变量赋值给包装类。
自动拆箱:可以把包装类的变量直接赋值给基本数据类型。

public class PackegeClass {
    public static void main(String[] args) {
        int a = 12 ;
        Integer a1 = 12 ;  // 自动装箱
        Integer a2 = a ;   // 自动装箱

        double b = 99.9;
        Double b1 = 99.9; // 自动装箱
        Double b2 = b ;   // 自动装箱

        Integer c = 100 ;
        int c1 = c ;      // 自动拆箱
/*************************重要区别******************/
        int d = 12;        //基本数据类型的d不能为null
        Integer d1 = null; // 引用数据类型的默认值可以为null
        Integer d2 = 0;


        Integer it = Integer.valueOf(12);  // 手工装箱!
        // Integer it1 = new Integer(12); // 手工装箱!
        Integer it2 = 12;
        Integer it3 = 111 ;
        int it33 = it3.intValue(); // 手工拆箱
        int it333 = it3;
    }
}

包装类的特殊功能

  1. Java为包装类做了一些特殊功能,以便程序员使用。
    包装类作为类首先拥有了Object类的方法。
    包装类作为引用类型的变量可以存储null值。

  2. 具体来看特殊功能主要有:

    1. 可以把基本数据类型的值转换成字符串类型的值。(没啥用)
      – 调用toString()方法。
      – 调用Integer.toString(基本数据类型的值)得到字符串。
      – 直接把基本数据类型+空字符串就得到了字符串。

    2. 把字符串类型的数值转换成对应的基本数据类型的值。(真的很有用)
      – Xxx.parseXxx(“字符串类型的数值”)
      – Xxx.valueOf(“字符串类型的数值”):推荐使用!

  3. 小结:
    包装类可以把字符串类型的数值转换成对应的基本数据类型的值(真的很有用)

public class PackageClass02 {
    public static void main(String[] args) {
        // 1.把基本数据类型的值转成字符串
        Integer it = 100 ;
        // a.调用toString()方法。
        String itStr = it.toString();
        System.out.println(itStr+1);
        // b.调用Integer.toString(基本数据类型的值)得到字符串。
        String itStr1 = Integer.toString(it);
        System.out.println(itStr1+1);
        // c.直接把基本数据类型+空字符串就得到了字符串。
        String itStr2 = it+"";
        System.out.println(itStr2+1);

        // 2.把字符串类型的数值转换成对应的基本数据类型的值。(真的很有用)
        String numStr = "23";
        //int numInt = Integer.parseInt(numStr);
        int numInt = Integer.valueOf(numStr);
        System.out.println(numInt+1);

        String doubleStr = "99.9";
        //double doubleDb = Double.parseDouble(doubleStr);
        double doubleDb = Double.valueOf(doubleStr);
        System.out.println(doubleDb+0.1);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沐鑫本鑫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值