BigDecimal类

1.Java提供一个中小数精确计算的类
  • 构造方法:
  •  public BigDecimal(String val)
    

*字段:

  •  public static final int ROUND_HALF_UP:舍入模式:四舍五入
    
  • 成员方法:

  •  public BigDecimal add(BigDecimal augend) :加法运算
    
  •  public BigDecimal substract(BigDecimal augend):减法
    
  •  public BigDecimal multiply(BigDecimal multiplicand):乘法
    
  •  public BigDecimal divide(BigDecimal divisor):除法
    
  •  public BigDecimal divide(BigDecimal divisor,
    
  •                      int scale,
    
  •                      RoundingMode roundingMode)
    
  •                   参数1:除数值
    
  •                   参数2: 小数点后保留的有效位数
    
  •                   参数3:舍入模式
    
  •                   RoundingMode:枚举类
    
  •                     HALF_UP :四舍五入
    
public static void main(String[] args) {
        BigDecimal bg=new BigDecimal("1.32");
        BigDecimal bg2=new BigDecimal("1.55");

       BigDecimal s1=bg.add(bg2);
        BigDecimal s2=bg.subtract(bg2);
        BigDecimal s3=bg.multiply(bg2);

        System.out.println(s1+""+s2+""+s3);
        System.out.println(bg.divide(bg2,3, RoundingMode.HALF_UP));
    }

2.Math类:abs求绝对值
  • Jdk5新特性:静态导入:导入到方法的级别,前提条件是方法必须静态

  •  注意:自定义的方法名不能和被导入的静态方法名冲突,否则报错!
    
import static java.lang.Math.abs ;
public class MathDemo {
    public static void main(String[] args) {
        int max = Math.max(10, 20);
        System.out.println(max);
       // int num = Math.abs(-100);
       // System.out.println(num);

        int number = java.lang.Math.abs(-100); //区分abs方法
        System.out.println(number);

    }
    public static void abs(){

    }
}
3.Random类

java.util.Random:随机数生成器

  • 构造方法
  • Random()
  •       创建一个新的随机数生成器。
    
  •       空参构造:通过这个构造:调用nextInt():  获取int类型的范围内的随机数
    
  • 调用nextInt(int n):0-n之间随机数
  •                         每一次产生的随机不相同!
    
  • Random(long seed):
  •      创建一个随机数生成器: 传递seed(初始种子)
    
  •              调用nextInt():  获取int类型的范围内的随机数
    
  • 调用nextInt(int n):0-n之间随机数
  •                  产生随机相同
    
public static void main(String[] args) {
        Random rd=new Random();
        for (int i=0;i<10;i++){
            int number= rd.nextInt(30)+1;
            System.out.println(number);
        }
    }
4.System类

java.lang.System:System 类包含一些有用的类字段和方法。它不能被实例化。

  • 常用的两个字段

  • public static final InputStream int ; 标准输入流

  •  public static final PrintStream out  ; 标准输出流
    
  • public static final PrintStream err ; 标准错误输出流

  • 常用方法:

  • public static void exit(int status):参数为0:正常终止jvm

  • public static void gc():手动开启垃圾回收器

  • 开启垃圾回收器,回收内存中没有更多引用的对象,节省当前对象所占用的空间

  • public static long currentTimeMillis():获取时间相关的毫秒数 (很少单独使用:计算一个程序的执行效率!)

    面试题
    final,finally,finalize的区别?

    final:状态修饰符(最终,无法更改的)
    修饰类,类不能被继承
    修饰方法,方法不能重写
    修饰变量,变量是一个常量

finalize是一个方法,是Object类的成员方法:
当前垃圾回收器开启的时候,回收内存中没有更多引用对象, 自动回收这些对象,以便节省被占用的内存空间!

​ 它是一个方法;跟gc有关系,当内存没有更多对象引用的时候,需要开启GC,由Object的finalize()回收
这些对象,将被占用的空间给释放!

public class SystemDemo {
    public static void main(String[] args) {
        //  public static void gc():手动开启垃圾回收器
        // 开启垃圾回收器,回收内存中没有更多引用的对象,节省当前对象所占用的空间
//        System.err.println();

        //创建一个学生类对象
        Student s = new Student("高圆圆",41) ;
        System.out.println(s) ;

        s = null ; //空对象:没有更多引用了
        System.gc() ;//手动开启垃圾回收器---->调用finalize()回收不用的对象(GC)

        System.out.println("-----------------------------------------------------");

        //  public static void exit(int status):参数为0:正常终止jvm
        /**
         * 应用场景:
         *      while(true){
         *
         *          嵌套了switch语句
         *                  break:可以结束switch语句
         *
         *                  使用这个功能:当前while循环到某个条件的时候,结束循环:System.exit(0) ;
         *      }
         */
        System.out.println("程序开始了");
        System.exit(0);//终止jvm----->main方法也就结束了...
        System.out.println("程序结束了");


    }
}
  • public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
  • 复制数组操作:
  • 参数1:原数组,
  • 参数2:从原数组的某个位置开始
  • 参数3:目标数组
  • 参数4:目标数组的目标位置
  • 参数5:复制的长度
public class SystemDemo2 {
    public static void main(String[] args) {

        //从arr1中复制一部分元素到arr2中
        //有两个数组,静态初始化
        int[] arr1  = {11,22,33,44,55} ;
        int[] arr2  = {1,2,3,4,5,6,7,8,9} ;

        System.out.println("复制之前:");
        //Arrays的toString(数组)
        System.out.println(Arrays.toString(arr1));
        System.out.println(Arrays.toString(arr2));

        System.out.println("-------------------------------");
        System.arraycopy(arr1,2,arr2,4,3);
        System.out.println(Arrays.toString(arr1));
        System.out.println(Arrays.toString(arr2));


    }
}

/*
*
* Arrays的toString方法源码:
* public static String toString(int[] a) {
        if (a == null)
            return "null";
        int iMax = a.length - 1;   //最大索引值 :arr1.length-1
        if (iMax == -1)
            return "[]";

        StringBuilder b = new StringBuilder(); //单线程程序中使用StringBuilder
        b.append('[');    //"["
        for (int i = 0; ; i++) {   //遍历arr1数组
            b.append(a[i]);   // 将所有元素追加进来
            if (i == iMax) //当前角标取到 最大索引值
                return b.append(']').toString();  //追加"]" 转换String
            b.append(", ");  //如果没有取到最大索引值,中间的元素 追加", "
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值