javaSE基础-其它常用类

其它常用类

System类

System类代表系统,系统级的很多属性和控制方法都放置在该类的内部,位于java.lang包。该类的构造器是private的,无法创建该类的对象。其成员变量和成员方法都是static的。

成员变量

static PrintStream err “标准”错误输出流

static InputStream in “标准”输入流

static PrintStream out “标准”输出流

常用成员方法

static long currentTimeMillis():返回当前时间以毫秒为单位,时间表达格式为当前计算机时间与GMT时间(格林威时间)1970年1月1日0时0分0秒之差的毫秒数。

void exit(int status):该方法的作用是退出程序,其status的值为0代表正常退出,非零代表异常退出。

void gc():该方法的作用是请求系统进行垃圾回收。

String getProperty(String key):该方法的作用是获得系统中的属性名为key的属性对应的值,常见的属性名及作用如表:

属性名 属性说明
java.version java运行时环境版本
java.home java安装目录
os.name 操作系统的名称
os.version 操作系统的版本
user.name 用户的账户名
user.home 用户的主目录
user.dir 用户的当前工作目录

示例:

@Test
public void test1(){
    String property = System.getProperty("user.home");
    System.out.println("user的home " + property);
}

Math类

java.lang.Math提供了一系列静态方法用于科学计算

abs:绝对值

acos、asin、atan、cos、sin、tan:三角函数

sqrt:平方根

pow(double a,double b):a的b次幂

log:自然对数

exp:e为底指数

max(double a,double b):最大的数

min(double a,double b):最小的数

random():返回0.0到1.0的随机数

long round(double a):double类型的数据a转为long类型数据,并四舍五入

toDegrees(double angrad):弧度-->角度

toRadians(double angdeg):角度 --> 弧度

BigInteger与BigDecimal

BigInteger类

Integer类:是int的包装类,存储的最大整型值231-1

Long类:存储的最大整型值263 -1

java.math.BigInteger类:该类表示不可变的任意精度的整数,除了java.lang.Math的所有相关方法,还提供了以下的运算:模算术、GCD计算、质数测试、素数生成、位操作等

构造器:

BigInteger(String val):根据字符串构建BigInteger对象

常用方法:

BigInteger abs()
返回的值BigInteger是BigInteger的绝对值。

BigInteger add(BigInteger val)
返回的值是 (this + val) BigInteger。

BigInteger subtract(BigInteger val)
返回的值是 (this - val) BigInteger。

BigInteger multiply(BigInteger val)
返回的值是 (this * val) BigInteger。

BigInteger divide(BigInteger val)
返回的值是 (this / val) BigInteger。

BigInteger remainder(BigInteger val)
返回的值是 (this % val) BigInteger。

BigInteger[] divideAndRemainder(BigInteger val)
返回两个关于大整数包含 (this / val)随后 (this % val)数组。

BigInteger pow(int exponent)
返回的值是 (thisexponent) BigInteger。

BigDecimal类

一般Float类和Double类可以用作科学计算或工程计算,商业计算中,要求数字比较高,需要java.math.BigDecimal类

BigDecimal类支持不可变的、任意精度的有符号十进制定点数

构造器:

public BigDecimal(double val)

public BigDecimal(String val)

常用方法:

BigDecimal add(BigDecimal augend)
返回的值是 BigDecimal (this + augend),其规模 max(this.scale(), augend.scale())。

BigDecimal subtract(BigDecimal subtrahend)
返回的值是 BigDecimal (this - subtrahend),其规模 max(this.scale(), subtrahend.scale())。

BigDecimal multiply(BigDecimal multiplicand)
返回的值是 BigDecimal (this × multiplicand),其规模 (this.scale() + multiplicand.scale())。

BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)
返回的值是 BigDecimal (this / divisor),其规模为指定的。

示例

@Test
public void test2(){
    BigInteger bi = new BigInteger("123333333333333454523");
    BigDecimal bd = new BigDecimal("1264.23421");
    BigDecimal bd2 = new BigDecimal("13");

    System.out.println(bi);//123333333333333454523

    //除以
    System.out.println(bd.divide(bd2));//没有指定保留的位数,报错
    System.out.println(bd.divide(bd2, BigDecimal.ROUND_HALF_UP));//97.24879
    System.out.println(bd.divide(bd2, 12, BigDecimal.ROUND_HALF_UP));//97.248785384615

}
posted @ 2022-11-05 21:09  Bingeone  阅读(18)  评论(0编辑  收藏  举报