6.系统相关的类
6.1 System
java.lang.System
6.1.1 属性
| 修饰符和类型 | 字段和说明 |
|---|---|
| static PrintStream | err “标准”错误输出流(显示器)。 |
| static InputStream | in “标准”输入流(键盘输入)。 |
| static PrintStream | out “标准”输出流(显示器)。 |
6.1.2 成员方法
- native long currentTimeMillis(),作用是:返回当前时间和GMT(格林威治标准时间:1970年 1月 1日 0时 0分 0秒)即UTC 之间的差值(以毫秒为单位)。
- void exit(int status),作用是:退出程序。status= 0,正常退出;非0,异常退出。使用该方法可以图形界面编程中实现程序退出的功能。
- void gc(),作用是:请求系统进行垃圾回收。至于系统是否立刻回收,取决于系统中垃圾回收算法的实现以及系统的执行是的情况。
- String getProperty(String key),作用是:获取系统中属性名为 key的属性对应的值。系统常见属性名以及属性的作用如下所示:
| 属性名 | 属性说明 |
|---|---|
| java.version | Java运行时环境版本 |
| java.home | Java安装目录 |
| os.name | 操作系统的名称 |
| os.version | 操作系统的版本 |
| user.name | 用户的帐户名称 |
| user.home | 用户的主目录 |
| user.dir | 用户的当前工作目录 |
/*** 1. getProperty()方法* 获取系统中属性名为 key的属性对应的值*/@Testpublic void test1(){String javaVersion = System.getProperty("java.version");System.out.println("java.version: "+javaVersion);String javaHome = System.getProperty("java.home");System.out.println("java.home: "+javaHome);String osName = System.getProperty("os.name");System.out.println("os.name: "+osName);String osVersion = System.getProperty("os.version");System.out.println("os.version: "+osVersion);String userName = System.getProperty("user.name");System.out.println("user.name: "+userName);String userHome = System.getProperty("user.home");System.out.println("user.home: "+userHome);String dir = System.getProperty("dir");System.out.println("用户的当前工作目录dir: "+dir);}
6.2 Runtime类
java.lang.Runtime:
- 每一个 Java应用程序都有一个
Runtime类实例,使应用程序能够与其运行的环境相连接。 - 它对应着 Java进程的内存使用的运行时环境,是
单例的。
6.2.1 Runtime方法:
public static Runtime getRuntime():返回与当前应用程序相关的运行时对象。应用程序不能创建自己的 Runtime类实例。
public native long freeMemory(); :返回 Java虚拟机中的空闲内存量。调用 gc()方法可能导致 freeMemory返回值的增加。
public native long totalMemory(); :返回 Java虚拟机中初始化时的内存总量。此方法的返回值可能随时间的推移而变化,这取决于主机环境。默认为电脑物理内存的 1/64。
public native long maxMemory();:返回 Java虚拟机中最大能使用的内存总量。默认为电脑内存的 1/4.
6.2.2 代码示例
/*** 2.Runtime类:获取系统内存的方法*/@Testpublic void test2() {Runtime runtime = Runtime.getRuntime();//返回 Java虚拟机中`初始化时的内存总量`long initialMemory = runtime.totalMemory();//返回 Java虚拟机中`最大能使用的内存总量`long maxMemory = runtime.maxMemory();String str = "";//模拟占用内存for (int i = 0; i < 10_000; i++) {str += i;}long freeMemory = runtime.freeMemory();System.out.println("初始化时总内存:" + initialMemory / 1024 / 1024 * 64 + "MB");System.out.println("最大可用总内存:" + maxMemory / 1024 / 1024 * 4 + "MB");System.out.println("空闲内存:" + freeMemory / 1024 / 1024 + "MB");System.out.println("已用内存:" + (initialMemory - freeMemory) / 1024 / 1024 + "MB");}
7. 和数学相关的类
7.1 java.long.Math类
- 是常见的数学运算类
- 常用:
- Math.runtime();//随机数
- Math.PI();常量π
**Math类常用方法: **
package com.lCommonClasses.cMathClass;public class Math1 {public static void main(String[] args) {System.out.println(Math.max(2,2.8));//获得最大值2.8System.out.println(Math.min(9.6,10));//获得最小值:9.6System.out.println(Math.abs(-20));//获得绝对值:20System.out.println(Math.random());//生成`0~1`的随机数//返回大于等于参数的最小整数。ceil天花板System.out.println(Math.ceil(3.3));//4.0//返回小于等于参数的最大整数。floor地板System.out.println(Math.floor(3.3));//3.0/**返回最接近参数的 long(相当于四舍五入法)*/System.out.println(Math.round(5.5));//6System.out.println(Math.round(5.4));//5//技巧:floor(x + 0.5)System.out.println(Math.round(-12.3));//-12System.out.println(Math.round(-12.6));//-13System.out.println(Math.round(-12.5));//-12//返回的是 2的3次方System.out.println(Math.pow(2,3));//8.0//返回 4的平方根System.out.println(Math.sqrt(4));//2.0//返回 圆周率PISystem.out.println(Math.PI);}
}
此外还有,三角函数:acos, asin, atan, cos, sin, tan
7.2 java.math包
7.2.1 BigInteger
- Integer作为 int的包装类,能存储的最大整型值为231-1,Long类也是有限的,最大为263-1。如果要表示再大的整数,不管是基本数据类型 还是他们的包装类都无能为力,更不用说进行运算了
- java.math包的 BigInteger可以表示
不可变的任意精度的整数。BigInteger提供所有 Java的基本整数操作符的对应物,并提供 java.lang.Math的所有相关方法。另外,BigInteger还提供以下运算:模运算、GCD计算、质数测试、素数生成、位操作以及一些其它操作。 - 构造器:
BigInteger(String val):根据字符串构建 BigInteger对象 - 方法:
public BigInteger abs():返回此 BigInteger 的绝对值的 BigInteger
BigIntegeradd(BigInteger val):返回其值为 (this + val) 的 BigInteger
BigIntegersubtract(BigInteger val):返回其值为 (this - val) 的 BigInteger
BigIntegermultiply(BigInteger val):返回其值为 (this * val) 的 BigInteger
BigIntegerdivide(BigInteger val):返回其值为 (this / val) 的 BigInteger
BigIntegerremainder(BigInteger val):返回其值为 (this % val) 的 BigInteger
BigInteger[]divideAndRemainder(BigInteger val):返回包含 (this / val) 后跟 (this % val) 的两个 BigInteger的数组
BigIntegerpow(int exponent):返回其值为 (this^exponent) 的 BigInteger
package com.lCommonClasses.cMathClass;
import org.junit.Test;
import java.math.BigInteger;/*** BigInteger 任意精度的整数*/
public class BigInteger1 {@Testpublic void test1() {BigInteger b1 = new BigInteger("12345678912345678912345678");BigInteger b2 = new BigInteger("546791234567891234567891234567");//System.out.println("和:"+(b1+b2));//错System.out.println("和:"+b1.add(b2));System.out.println("减:"+b1.subtract(b2));System.out.println("乘:"+b1.multiply(b2));System.out.println("除:"+b1.divide(b2));System.out.println("余:"+b1.remainder(b2));}
}
7.2.2 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): divisor,除数;scale,指明保留几位小数;roundingMode,指明舍入方式(ROUND_UP, 向上 +1;ROUND_DOWN, 直接舍去;ROUNDOM_HALF_UP, 四舍五入)
package com.lCommonClasses.cMathClass;
import org.junit.Test;
import java.math.BigDecimal;/*** BigDecimal 任意精度的浮点数*/
public class BigDecimal1 {@Testpublic void test1(){BigDecimal bi = new BigDecimal("123456789.123");BigDecimal bd = new BigDecimal("123456789.123");BigDecimal bd2 = new BigDecimal("11");System.out.println(bi);/*报ArithmeticException:因为BigDecimal的divide方法出现了无限循环小数System.out.println(bd.divide(bd2));解决方法 如下,两种方法:*/System.out.println(bd.divide(bd2,BigDecimal.ROUND_HALF_UP));//divisor,除数;scale,指明保留几位小数;roundingMode,指明舍入方式System.out.println(bd.divide(bd2,15,BigDecimal.ROUND_DOWN));}
}
7.2.3. java.util.Random
- Rundom类 —— 用于生成随机数
- 方法
boolean nextBoolean():返回下一个伪随机数,它是取自此随机数生成器序列的 均匀分布的 boolean值void nextBoolean(byte[] bytes):生成随机字节并将其置于用户提供的 byte数组中。double nextDouble():返回下一个伪随机数,它是取自此随机数生成器序列的、在 0.0 ~ 1.0 之间均匀分布的 double值。float nextFloat():返回下一个伪随机数,它是取自此随机数生成器序列的、在 0.0 ~ 1.0 之间均匀分布的 float值。double nextGaussian():返回下一个伪随机数,它是取自此随机数生成器序列的、呈高斯(“正太”)分布的 double值,其平均值是0.0,标准差是1.0。int nextInt():返回下一个伪随机数,它是随机数生成器的序列中均匀分布的 int值。int nextInt(int n):返回下一个伪随机数,它是随机数生成器序列的、在 0(包含)和指定值 n(不包含) 之间均匀分布的 int值。(用得较多)long nextLong:返回下一个伪随机数,它是随机数生成器的序列中均匀分布的 long值。
package com.lCommonClasses.cMathClass;import java.util.Random;
import java.util.UUID;public class Random1 {public static void main(String[] args) {/**1.Random*/Random random = new Random();int captcha = random.nextInt(10000);float randomNumbers = random.nextFloat();System.out.println("四位验证码是:"+captcha);System.out.println("随机数是:"+randomNumbers);/**2.UUID:是指通用唯一识别码* (Universally Unique Identifier)*/String id= UUID.randomUUID().toString();//生成随机IDSystem.out.println(id);}
}