Math类:
代表数学,是一个工具类;
public static int abs(int a)
Returns the absolute value of an
int
value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned.取绝对值
public class test {public static void main(String[] args) {System.out.println(Math.abs(-23));//23System.out.println(Math.abs(-4.3));//4.3System.out.println(Math.abs(2));//2}
}
public static double ceil(double a)
Returns the smallest (closest to negative infinity)
double
value that is greater than or equal to the argument and is equal to a mathematical integer。向上取整
System.out.println(Math.ceil(4.0001));//5.0System.out.println(Math.ceil(3.0));//3.0
public static double floor(double a)
Returns the largest (closest to positive infinity)
double
value that is less than or equal to the argument and is equal to a mathematical integer.向下取整
System.out.println(Math.floor(3.999));//3.0
public static long round(double a)
Returns the closest
long
to the argument, with ties rounding to positive infinity.四舍五入
System.out.println(Math.round(3.56));//4
public static int max(int a, int b)
Returns the greater of two
int
values.取最大值
public static int min(int a, int b)
Returns the smaller of two
int
values.取最小值
public static double pow(double a, double b)
幂运算
System.out.println(Math.pow(2,3));//8.0
public static double random()
Returns a
double
value with a positive sign, greater than or equal to0.0
and less than1.0
.返回0.0到1.0的数,不包括1.0
System.out.println(Math.random());//0.8425849812213428
System类:代表程序所在的系统,也是一个工具类
public static void exit(int status)
Initiates the shutdown sequence of the Java Virtual Machine.
终止当前运行的JVM虚拟机
status非零表示异常终止
public static long currentTimeMillis()
Returns the current time in milliseconds.
获取当前系统的时间,返回的是毫秒值,返回的是从1970-1-1 0:0:0开始到现在的毫秒值。1s=1000ms
Runtime类:代表程序所在的运行环境
Runtime是一个单例类
public void exit(int status)
Initiates the shutdown sequence of the Java Virtual Machine.
public int availableProcessors()
Returns the number of processors available to the Java virtual machine.
获取虚拟机能够使用的处理器数
public class test {public static void main(String[] args) {Runtime r=Runtime.getRuntime();System.out.println(r.availableProcessors());//16}
}
public long totalMemory()
Returns the total amount of memory in the Java virtual machine.
返回虚拟机的内存总量
System.out.println(r.totalMemory()/1024.0+"KB");//1024=1KBSystem.out.println(r.totalMemory()/1024.0/1024.0+"MB");//252.0MB
//freeMemory()返回虚拟机的可用内存量System.out.println(r.freeMemory()/1024.0/1024.0+"MB");//246.67790985107422MB
public Process exec(String command) throws IOException
启动某个程序,并返回代表程序的对象
Process p=r.exec("QQ");Thread.sleep(4000);//让当前程序运行4sp.destroy();//销毁程序
BigDecimal类:用于解决浮点型运算时,出现结果失真的问题
public BigDecimal(String val)
Translates the string representation of a
BigDecimal
into aBigDecimal
.把字符串变成BigDecimal类型
public class test {public static void main(String[] args) {double a=0.1;double b=0.2;//1:把他们变成字符串再变成Bigdecimal类型/*BigDecimal a1=new BigDecimal(Double.toString(a));BigDecimal b1=new BigDecimal(Double.toString(b));*/BigDecimal a1=BigDecimal.valueOf(a);//在底层调用了上面的构造器,更简洁BigDecimal b1=BigDecimal.valueOf(b);BigDecimal c1=a1.add(b1);System.out.println(c1.toString());//0.3BigDecimal c2=a1.subtract(b1);//减法System.out.println(c2);//-0.1BigDecimal c3=a1.multiply(b1);//乘法System.out.println(c3);//0.02//BigDecimal c4=a1.divide(b1);//除法System.out.println(c4);//0.5BigDecimal i=BigDecimal.valueOf(0.1);BigDecimal j=BigDecimal.valueOf(0.3);
// BigDecimal k=i.divide(j);//error,无限循环小数BigDecimal k=i.divide(j,2,RoundingMode.HALF_UP);//两位小数,以四舍五入的方式System.out.println(k);//0.33double x=k.doubleValue();//将BigDecimal类型变成double类型System.out.println(k);}
}