一、Math类
-
概述
- Math类中包含了用于执行基本数学运算的方法,如指数、对数、三角函数、开根等
-
成员变量
- public static final double E
- 自然底数
- public static final double PI
- 圆周率
- public static final double E
-
成员方法
- public static int abs (int a)
- 取a的绝对值
- public static double ceil(double a)
- 向上取整
- public static double floor(double a)
- 向下取整
- public static int max(int a,int b)
- 获取最大值
- public static int min(int a,int b)
- 获取最小值
- public static double pow(double a,double b)
- 求a的b次方,(求平方根时b为1/2)
- public static double random()
- 返回一个double类型的随机数,范围在0.0-1.0,不包含1.0
- public static int round(float a)
- 四舍五入
- public static double sqrt(double a)
- 获取a的正平方根
- 注意:Math方法没有实例,所有的变量和方法都是静态的,用类名调用
- public static int abs (int a)
二、Random类
-
概述
- 用于产生随机数
-
构造方法
- public Random()
- 默认无参构造,随机产生数
- public Random(long seed)
- 有参构造,给定一个同一个long类型的种子,以后每一次生成的随机数都是相同的
- public Random()
-
成员方法
- public int nextInt()
- 得到一个随机整数,范围是int类型的表示范围
- public int nextInt(int n)
- 得到一个随机整数,范围是0-(n-1)之间
- public void nextBytes(byte[] bytes)
- 产生随机字节存放于用户提供的空字节数组中
- public int nextInt()
三、System类
- 概述
- 不能实例化,提供输入输出、对外部属性与环境变量的访问、加载文件与库的方法等
- 成员方法
- public static void gc()
- 调用垃圾回收器,但是不能保证立即回收,只能催促一下,具体是否回收取决于JVM的垃圾回收机制
- public static void exit(int status)
- 退出虚拟机,0为正常结束,非0为异常退出
- public static void gc()
四、BigDecimal类
- 概述
- 在日常计算时,一般的float或double类型已经能够满足需求,但是在面对银行等对精度要求特别高的场景时就显得不够用了,这时候就需要精度更高的数据存储格式,BigDecimal类就是其中一个
- 构造方法
- public BigDecimal(String val)
- 将指定的字符串类型的参数传入创建的对象中
- public BigDecimal(String val)
- 成员方法
- public BigDecimal add(BigDecimal augend)
- 将两个对象的值相加
- public BigDecimal subtract(BigDecimal subtrahend)
- 将两个对象的值相减
- public BigDecimal multiply(BigDecimal multiplicand)
- 将两个对象的值相乘
- public BigDecimal divide(BigDecimal divisor)
- 将两个对象的值相除
- public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)
- 将两个对象相除,可任意选择位数及取舍模式,scale是位数,roundingMode是取舍模式
- public BigDecimal add(BigDecimal augend)
五、BigInteger类
- 概述
- 任意精度的整数,提供所有java.long.Math类下的方法,并提供模运算、质数测试、素数生成、位操作等其他方法
- 构造方法
- public BigInteger(String val)
- 将BigInteger型数据的字符串表示形式转换为BigInteger类型
- public BigInteger(String val)
- 成员方法
- public BigInteger add(BigInteger val)
- 加法
- public BigInteger subtract(BigInteger val)
- 减法
- public BigInteger multiply(BigInteger val)
- 乘法
- public BigInteger divide(BigInteger val)
- 除法
- public BigInteger[] divideAndRemainder(BigInteger val)
- 将两个BigInteger类的数据相除,将商和余数存进数组里面,并返回
- public BigInteger add(BigInteger val)
- 求10000/3的商和余数
public static void main(String[] args) {
BigInteger bigInteger1 = new BigInteger("10000");
BigInteger bigInteger2 = new BigInteger("3");//创建两个BigInteger类对象将除数和被除数存储起来
BigInteger[] arr = bigInteger1.divideAndRemainder(bigInteger2);//进行运算,并返回商和余数
System.out.println(Arrays.toString(arr));//打印商和余数
}
//运行结果为:[3333, 1]