个人简介
- 大家好,我是翰慧腾。一名正在努力学JAVA的大一小白,本文章为初学的笔记,希望各位多多指教。💙
- 欢迎点赞+收藏+留言💜
- 祝愿所有的程序员Bug少一点,头发多一点🧡
一、Math类
- Math类是在java.lang包下,可以直接使用,不需要导包。该类中的方法大多是static修饰,直接用类名.方法调用,不需要创建对象。
- 该类属于是工具类,为我们提供了大量的数学领域用到的方法。在以后的实际开发中,若需要数学中的某些处理可以去API文档查询该类。
常用方法:
1.abs方法:获取参数绝对值。
API文档:
源码:
public static int abs(int a) {
return (a < 0) ? -a : a;
}
2.ceil方法:向上取整。
API文档:
源码:
public static double ceil(double a) {
return StrictMath.ceil(a); // default impl. delegates to StrictMath
}
3.floor方法:向下取整。
API文档:
源码:
public static double floor(double a) {
return StrictMath.floor(a); // default impl. delegates to StrictMath
}
4.pow方法:求指数次方
API文档:
源码如下:
public static double pow(double a, double b) {
return StrictMath.pow(a, b); // default impl. delegates to StrictMath
}
5.round方法:对参数四舍五入
API文档:
提醒:NaN是一个值类型,也是一个数值,是一个与任何数值都不想等的数值。
源码如下:
public static long round(double a) {
long longBits = Double.doubleToRawLongBits(a);
long biasedExp = (longBits & DoubleConsts.EXP_BIT_MASK)
>> (DoubleConsts.SIGNIFICAND_WIDTH - 1);
long shift = (DoubleConsts.SIGNIFICAND_WIDTH - 2
+ DoubleConsts.EXP_BIAS) - biasedExp;
if ((shift & -64) == 0) { // shift >= 0 && shift < 64
// a is a finite number such that pow(2,-64) <= ulp(a) < 1
long r = ((longBits & DoubleConsts.SIGNIF_BIT_MASK)
| (DoubleConsts.SIGNIF_BIT_MASK + 1));
if (longBits < 0) {
r = -r;
}
6.random方法:产生一个随机数
API文档:
源码如下:
public static double random() {
return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble();
}
产生随机数方法:该方法无参数,默认的是[0.0,1.0),注意是取不到1.0的。
eg:产生一个 [1,100]随机数:(int)(Math.random()*100)+1
二、System类
概述:System类也是通用的,直接用类名调用即可,不能创建对象。方法大多数是静态的,可以直接调用。
1.exit方法(了解即可):用于终止当前运行的JAVA虚拟机,通常参数为0
API文档:
源码如下:
public static void exit(int status) {
Runtime.getRuntime().exit(status);
}
2.currentTimeMillis方法(了解即可):返回当前时间的毫秒值(从1970-1-1 00:00:00到现在的毫秒值)
API文档:
源码如下:
@IntrinsicCandidate
public static native long currentTimeMillis();
应用:用于检验电脑性能,可以判断你的某个循环花费了多长时间
package Stickto;
public class System_ {
public static void main(String[] args) {
long s1=System.currentTimeMillis();
for(int i=1;i<100000;i++) {
System.out.println(i);
}
long s2=System.currentTimeMillis();
System.out.println("输出100000个数所花费时间:"+(s2-s1)/1000.0);//把时间转化成秒为单位
}
}
3.arraycopy方法:用于数组的拷贝
API文档:
源码如下:
@IntrinsicCandidate
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
三、BigDecimal类
概述:用于解决浮点型运算精度失真的问题 。在实际开发中,禁止使用构造方法BigDecimal(double)的方式把double值转化为BigDecimal对象,推荐使用参数为String的构造方法,或者使用BigDecimal类中的valueOf方法。
1.valueOf方法(常用):将浮点型数转化为BigDecimal型对象。
API文档:
源码如下:
public static BigDecimal valueOf(double val) {
// Reminder: a zero double returns '0.0', so we cannot fastpath
// to use the constant ZERO. This might be important enough to
// justify a factory approach, a cache, or a few private
// constants, later.
return new BigDecimal(Double.toString(val));
}
2.add方法:用于BigDecimal型求和
API文档:
源码如下:
public BigDecimal add(BigDecimal augend) {
if (this.intCompact != INFLATED) {
if ((augend.intCompact != INFLATED)) {
return add(this.intCompact, this.scale, augend.intCompact, augend.scale);
} else {
return add(this.intCompact, this.scale, augend.intVal, augend.scale);
}
} else {
if ((augend.intCompact != INFLATED)) {
return add(augend.intCompact, augend.scale, this.intVal, this.scale);
} else {
return add(this.intVal, this.scale, augend.intVal, augend.scale);
}
}
}
3.doubleValue方法:将BigDecimal运算后的数组转化为double 型的。
API文档:
源码如下:
@Override
public double doubleValue(){
if(intCompact != INFLATED) {
if (scale == 0) {
return (double)intCompact;
} else {
/*
* If both intCompact and the scale can be exactly
* represented as double values, perform a single
* double multiply or divide to compute the (properly
* rounded) result.
*/
if (Math.abs(intCompact) < 1L<<52 ) {
// Don't have too guard against
// Math.abs(MIN_VALUE) because of outer check
// against INFLATED.
if (scale > 0 && scale < DOUBLE_10_POW.length) {
return (double)intCompact / DOUBLE_10_POW[scale];
} else if (scale < 0 && scale > -DOUBLE_10_POW.length) {
return (double)intCompact * DOUBLE_10_POW[-scale];
}
}
}
}
// Somewhat inefficient, but guaranteed to work.
return Double.parseDouble(this.toString());
}