- System类
成员变量:
out PrintStream标准输入流(键盘输入),
in InputStream 标准输出流(显示器),
error PrintStream 标准错误输出流(显示器)
成员方法:
native long currentTimeMillis(): 返回当前的计算机时间(与1970/1/1/0/0/0所差的毫秒数)
void exit(int status):该方法的作用是退出程序。其中status的值为0代表正常退出,非零代表异常退出。
void gc():该方法的作用是请求系统进行垃圾回收。值与系统是否立即回收,取决于系统中垃圾回收算法的实现及系统执行时的情况。
String getProperty(String key): 该方法的作用是获得系统中属性名为key的属性对应的值。系统中常见的属性名以及属性的作用如下所表示:
举例
package Arrays;
import org.junit.Test;
public class OtherAPItest {
public static void main(String[] args) {
}
@Test
public void test01(){
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.name");
System.out.println("user的home:"+userHome);
String userDir = System.getProperty("user.name");
System.out.println("user的Dir:"+userDir);
}
}
显示输出
java的version:22
java的home:/home/dx/.jdks/openjdk-22
os的name:Linux
os的version:6.5.0-35-generic
user的name:dx
user的home:dx
user的Dir:dx
Process finished with exit code 0
- Runtime类
java.lang.Runtime类
每个java应用程序都有一个Runtime类实例,使应用程序能够与其运行的环境相连接。
public static Runtime getRuntime()
返回与当前java应用程序相关的运行时对象。应用程序不能创建自己的Runtime类实例。
public long totalMemory()
返回Java虚拟机中初始化时的内存总量,此方法返回的值可能随时间的推移而变化,取决于主机环境,默认为电脑物理内存的1/64.
public long maxMemory()
返回Java虚拟机中最大程度能使用的内存总量,默认为物理电脑内存的1/4
public long freeMemory()
返回Java虚拟机中的空闲内存量。调用gc方法可能导致freeMemory返回值的增加。
对应着Java进程的内容使用的运行时环境,是单例的
举例
package Arrays;
import org.junit.Test;
public class OtherAPItest {
@Test
public void RunTimetest(){
Runtime runtime = Runtime.getRuntime();
long initialMem= runtime.totalMemory();//获取虚拟机初始化时候堆内存总量
long maxMem = runtime.maxMemory();//获取虚拟机最大堆内存总量
String str = "";
//模拟占用内存
for (int i = 0; i < 10000; i++) {
str += i;
}
long freeMem = runtime.freeMemory();//获取空闲堆内存总量
System.out.println("总内存:"+initialMem /1024/1024*64 + "MB"); //totalMemory()默认为电脑物理内存的1/64.所以乘64,粗略计算电脑物理内存
System.out.println("总内存:"+maxMem /1024/1024*4 + "MB");//maxMemory()默认返回物理电脑内存的1/4,想计算电脑物理内存,乘4
System.out.println("空闲内存:"+freeMem /1024/1024 + "MB");
System.out.println("已用内存:"+(initialMem-freeMem) /1024/1024 + "MB");
}
}
结果输出
总内存:8064MB
总内存:7824MB
空闲内存:122MB
已用内存:3MB
Process finished with exit code 0
- Math类 (知识复习补充)
publc static double abs(double a)
: 返回double值的绝对值
double d1 = Math.abs(-5); //d1的值为5
double d2 = Math.abs(5); //d2的值为5
publc static double ceil(double a)
: 返回大于等于参数的最小整数
double d1 = Math.ceil(3.3); //d1的值为4.0
double d2 = Math.ceil(-3.3); //d2的值为-3.0
double d3 = Math.ceil(5.1); //d3的值为6.0
public static double floor(double a)
:返回小与等于参数最大的整数
double d1 = Math.floor(3.3); //d1的值为3.0
double d2 = Math.floor(-3.3); //d1的值为-4.0
double d3 = Math.floor(5.1); //d1的值为5.0
public static long round(double a)
:返回最接近参数的long,相当于四舍五入
long d1 = Math.round(5.5); //d1的值为6
long d2 = Math.round(5.4); //d2的值为5
long d3 = Math.round(-3.3); //d3的值为-3
long d4 = Math.round(-3.8); //d4的值为-4
举例
package Arrays;
import org.junit.Test;
public class OtherAPItest {
@Test
public void test3(){
System.out.println(Math.round(12.3));//得到12
System.out.println(Math.round(12.5));//得到13
System.out.println(Math.round(-12.3));//得到-12
System.out.println(Math.round(-12.6));//得到-13
System.out.println(Math.round(-12.5));//得到-12 注意,当X.5的时候,取大的那个值。
}
}
public static double pow(double a, double b) :返回a的b幂次方
public static double sqrt(double a):返回a的平方根
public static double random():返回(0,1)的随机值
public static final double PI: 返回圆周率
public static double max(double x, double y):返回x,y中的最大值
public static double min(double x, double y):返回x,y中的最小值
其他:acos, asin, atan, cos, sin, tan三角函数
package Arrays;
import org.junit.Test;
public class OtherAPItest {
@Test
public void test3(){
double result = Math.pow(2,8);//计算2的8次方
double sqrt = Math.sqrt(256);//开根号
double rand = Math.random();//0-1之间随机数
double pi= Math.PI;
System.out.println(result);
System.out.println(sqrt);
System.out.println(rand);
System.out.println(pi);
}
}
运行结果:
256.0
16.0
0.8956979603883279
3.141592653589793
Process finished with exit code 0
- BigInteger类和BigDecimal类
BigInteger可以表示任意长度的整数
BigDecimal可以表示任意精度的浮点数
java.math包介绍
BigInteger
-
Integer类作为int的包装类,能存储得最大整型值为2^31-1, Long类也是有限的,最大为2^63-1.如果要表示更大的整数,基本数据类型还是包装类都无法覆盖了。
-
java.math包的BigInteger可以表示不可变的任意精度的整数。
BigInteger提供所有Java的基本整数操作运算符的对应物,并提供java.lang.Math的所有相关方法。另外,BigInteger还提供以下运算:模算术,GCD计算,质数测试,素数生成,位操作以及一些其他操作。 -
构造器
BigInteger(String val)
: 根据字符串构建BigInteger对象 -
方法
public 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的数组。
BigInteger pow(int exponent):返回其值为(this ^ exponent)的BigInteger。
package Arrays;
import org.junit.Test;
import java.math.BigDecimal;
import java.math.BigInteger;
public class OtherAPItest {
@Test
public void testBigInteger(){
//long bigNum = 123456789123456789123456789L;直接使用long已经无法
BigInteger b1 = new BigInteger("12345678912345678912345678");
BigInteger b2 = new BigInteger("789123456789123456781234561234");
//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("除:"+b2.divide(b1));
System.out.println("余:"+b2.remainder(b1));
}
}
运算结果:
和:789135802468035802460146906912
减:-789111111110211111102322215556
乘:9742264819718808029597956748221933300845390098066246652
除:63919
余:6390900006383011169152
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:直接舍去, ROUND_HALF_UP:四舍五入)
package Arrays;
import org.junit.Test;
import java.math.BigDecimal;
import java.math.BigInteger;
public class OtherAPItest {
@Test
public void BigDecimaltest(){
BigInteger bi = new BigInteger("123456789");
BigDecimal bd = new BigDecimal("123456789.351");
BigDecimal bd2 = new BigDecimal("11");
System.out.println(bi);
// System.out.println(bd.divide(bd2));
System.out.println(bd.divide(bd2,BigDecimal.ROUND_HALF_UP)); //ROUND_HALF_UP四舍五入
System.out.println(bd.divide(bd2,15,BigDecimal.ROUND_HALF_UP));//指定小数点保留位数=15。ROUND_HALF_UP四舍五入
}
}
结果显示:
123456789
11223344.486
11223344.486454545454545
Process finished with exit code 0
- Random类
java.util.Random 获取指定范围内的随机整数 nextInt(int bound)
package Arrays;
import org.junit.Test;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Random;
public class OtherAPItest {
@Test
public void Randomtest(){
Random random = new Random();
int i = random.nextInt();//生成均匀分布的随机int值
System.out.println(i);
int j = random.nextInt(10);//随机获取[0,10)范围的整数,注意随机数会小于10
System.out.println(j);
}
}
运行结果:
1973935791
2
Process finished with exit code 0
boolean nextBoolean()
:返回下一个伪随机数,它是取自此随机数生成器序列的均匀分布的boolean值。
void nextBytes(byte[] bytes)
:生成随机字节并将其放在用户提供的byte数组中
double nextDouble()
:返回下一个伪随机数,它是取自此随机数生成器序列的,在0和1之间均匀分布的double值。
float nextFloat()
:返回下一个伪随机数,它是取自此随机数生成器序列的,在0.0和1.0之间均匀分布的float值。
double nextGaussian()
:返回下一个伪随机数,它是取自此随机数生成器序列的,呈高斯(“正态”)分布的double值,其平均值是0.0,标准差是1.0.
int nextInt()
: 返回下一个伪随机数,它是此随机数生成器的序列中均匀分布的int值。
long nextLong()
:返回下一个伪随机数,它是取自此随机数生成器序列的均匀分布的long值。