java常用类:System+Math+BigInteger+BigDecimal
1 System类
System类代表系统,系统级的很多是属性和控制方法都放置在该类的内部,该类位于java.lang包。
由于该类的构造器是private的,所以无法创建该类的对象,也就是无法实例化该类。其内部的成员变量和成员方法都是static的,所以可以很方便的进行调用。
成员变量:
System类内部包含in、out、err三个成员变量,分别代表标准输入流(键盘输入),标准输出流(显示器)和标准错误输出流(显示器)
成员方法
native 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.versoin 操作系统的版本
user.name 用户的账户名称
user.home 用户的主目录
user.dir 用户的当前工作目录
1.1 public static native long currentTimeMillis()方法的使用
long time1=System.currentTimeMillis();
System.out.println("我是小徐");
Thread.sleep(203);
long time2=System.currentTimeMillis();
System.out.println("间隔时间:"+(time2-time1));
我是小徐
间隔时间:203
1.2 String getProperty(String key) 方法的使用
Consumer< T >可以根据类型T,并且方法是没有返回值的(适合打印的时候使用)
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
public class sys1 {
public static void main(String[] args) {
List<String> keys= Arrays.asList("java.version","java.home",
"os.name","os.version",
"user.name","user.home","user.dir");
//第一种方式:
System.out.println("第一种方式:");
Supp s=a -> System.out.println(System.getProperty(a));
for (String key : keys) {
s.print(key);
}
//第二种方式:
System.out.println("第二种方式:");
Consumer<String> k=d->System.out.println(System.getProperty(d));
keys.forEach(k);
}
}
@FunctionalInterface
interface Supp{
void print(String a);
}
2 Math类
java.lang.Math提供了一系列静态方法用于科学计算。其方法的参数和返回值类型一般为double型。
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型(四舍五入)
3 BigInteger
Integer类作为int的包装类,能存储的最大整数型值为2^31-1,Long类也是有限的,最大为2 ^63-1。如果要表示再大的整数,不管是基本数据类型还是他们的包装类都无能为力,更不用说进行运算。
java.math包的BigInteger可以表示不可变的任意精度的整数。BigInteger提供所有java的基本整数操作符的对应物,并提供java.lang.Math的所有相关方法。另外,BigInteger还提供以下运算:模算术、GCD计算、质数测试、素数生成、位操作以及一些其他操作。
构造器:BigInteger(String val):根据字符串构建BigInteger对象
常用方法
3.1 public BigInteger abs():返回此BigInteger的绝对值的BigInteger.
3.2 BigInteger add(BigInteger val):返回其值为(this+val)的BigInteger
3.3 BigInteger subtract(BigInteger val):返回此值为(this-val)的BigInteger
3.4 BigInteger multiply(BigInteger val):返回其值为(this*val)的BigInteger
3.5 BigInteger divide(BigInteger val):返回其值为(this/val)的BigInteger。整数相除只保留整数部分。
3.6 BigInteger remainder(BigInteger val):返回其值为(this%val)的BigInteger。
3.7 BigInteger divideAndRemainder(BigInteger val):返回包含(this/val)后跟(this%val)的两个BigInteger的数组。
3.8 BigInteger pow(int exponent):返回其值为(this ^ exponent)的BigInteger
BigInteger的取值必须是整数int类型:
import java.math.BigInteger;
public class Num {
public static void main(String[] args) {
BigInteger a=new BigInteger("8");
BigInteger b=new BigInteger("6");
System.out.println(a.add(b));
System.out.println(a.add(b).getClass());
}
}
14
class java.math.BigInteger
4 BigDecimal
一般的Float类和Double类可以用来做科学计算或工程计算,但在商业计算中,要求数字精度比较高,故而用到java.math.BigDecimal类。
BigDecimal类支持不可变的、任意精度的有符号十进制定点数。
构造器:
public BigDecimal(double 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, int scale, int roundingMode)
public static void main(String[] args) {
BigDecimal x= new BigDecimal("5.00");
BigDecimal y=new BigDecimal("3");
System.out.println(x.add(y));
System.out.println(x.subtract(y));
System.out.println(x.multiply(y));
//四舍五入,保留两位小数
System.out.println(x.divide(y,BigDecimal.ROUND_HALF_UP));
System.out.println(5.0/3.0);
//scale:保留多少位小数
System.out.println(x.divide(y,4,BigDecimal.ROUND_HALF_UP));
}
8.00
2.00
15.00
1.67
1.6666666666666667
1.6667
BigDecimal x= new BigDecimal("5.00");
BigDecimal y=new BigDecimal("3");
//能除尽就不会报错
System.out.println(x.divide(y));