Java 其他类API
文章目录
系统相关类
java.lang.System类
System类代表系统,系统级的很多属性和控制方法都放置在该类的内部。该类位于java.lang包
。
成员变量
System类内部包含in
、out
和err
三个成员变量,分别代表标准输入流(键盘输入),标准输出流(显示器)和标准错误输出流(显示器)。
static PrintStream | **[err](../../java/lang/System.html#err)** “标准”错误输出流。 |
---|---|
static InputStream | **[in](../../java/lang/System.html#in)** “标准”输入流。 |
static PrintStream | **[out](../../java/lang/System.html#out)** “标准”输出流 |
成员方法
- `native long currentTimeMillis()`:该方法的作用是返回当前的计算机时间,时间的表达格式为当前计算机时间和GMT时间(格林威治时间)1970年1月1号0时0分0秒所差的毫秒数。
- `void exit(int status)`:该方法的作用是退出程序。其中status的值为0代表正常退出,非零代表异常退出。使用该方法可以在图形界面编程中实现程序的退出功能等。
- `void gc()`:该方法的作用是请求系统进行垃圾回收。至于系统是否立刻回收,则取决于系统中垃圾回收算法的实现以及系统执行时的情况。
- `String getProperty(String key)`:该方法的作用是获得系统中属性名为key的属性对应的值。
* `static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)`: 从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。常用于数组的插入和删除
-
String getProperty(String key)
获取对应属性名key的属性的对应值常见的属性名及其对应作用:
属性名 说明 java.version java运行时环境版本 java.home Java安装目录 os.name 操作系统的名称 os.version 操作系统的版本 user.name 用户的账户名称 user.home 用户的主目录 user.dir 用户的当前工作目录 public class Test{ public static void main(String []args){ System.out.println(System.getProperty("java.version")); System.out.println(System.getProperty("java.version.date")); System.out.println(System.getProperty("java.vendor")); System.out.println(System.getProperty("java.vendor.url")); System.out.println(System.getProperty("java.vendor.version")); System.out.println(System.getProperty("java.home")); System.out.println(System.getProperty("java.compiler")); System.out.println(System.getProperty("os.name")); System.out.println(System.getProperty("user.name")); System.out.println(System.getProperty("user.home")); } } //21.0.2 //2024-01-16 //Oracle Corporation //https://java.oracle.com/ //null //D:\Java JDK //null //Windows 11 //27813 //C:\Users\27813
java.lang.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 返回值的增加。
public class Test{
public static void main(String []args){
Runtime runtime = Runtime.getRuntime();
long initialMemory = runtime.totalMemory(); //获取虚拟机初始化时堆内存总量
long maxMemory = runtime.maxMemory(); //获取虚拟机最大堆内存总量
String str = "";
//模拟占用内存
for (int i = 0; i < 10000; 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");
}
}
//输出结果
总内存:15616MB
总内存:15592MB
空闲内存:112MB
已用内存:131MB
数学相关类
java.lang.Math
java.lang.Math
类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。类似这样的工具类,其所有方法均为静态方法,并且不会创建对象,调用起来非常简单。
`public static double abs(double a) ` :返回 double 值的绝对值。
`public static double ceil(double a)` :返回大于等于参数的最小的整数。
`public static double floor(double a) ` :返回小于等于参数最大的整数。
`public static long round(double a)` :返回最接近参数的 long。(相当于四舍五入方法)
* 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 三角函数
public static double abs(double a)
:返回 double 值的绝对值。
public class AbsTest{
public static void main(String []args){
System.out.println(Math.abs(-5));
System.out.println(Math.abs(-7));
System.out.println(Math.abs(-6));
}
}
//输出结果
5
6
7
-
public static double ceil(double a)
:返回大于等于参数的最小的整数。public class CeilTest{ public static void main(String []args){ //向上取整 System.out.println(Math.ceil(3.5)); System.out.println(Math.ceil(3.4)); System.out.println(Math.ceil(3.9)); } } //输出结果 //4.0 //4.0 //4.0
-
public static double floor(double a)
:返回小于等于参数最大的整数。public class FloorTest{ public static void main(String []args){ System.out.println(Math.floor(3.5)); System.out.println(Math.floor(3.4)); System.out.println(Math.floor(3.9)); } } //输出结果 //3.0 //3.0 //3.0
-
public static long round(double a)
:返回最接近参数的 long。(相当于四舍五入方法)public class Test{ public static void main(String []args{ System.out.println(Math.round(3.4)); System.out.println(Math.round(3.5)); } } //输出结果 //3.0 //4.0
-
public static double pow(double a,double b)
:返回a的b幂次方法public class PowTest{ public static void main(String []args){ System.out.println(Math.pow(2,0)); System.out.println(Math.pow(2,2)); System.out.println(Math.pow(2,3)); } } //输出结果 //1.0 //4.0 //8.0
- 其他
double result = Math.pow(2,31); double sqrt = Math.sqrt(256); double rand = Math.random(); double pi = Math.PI;
java.math包
BigInteger
可以表示任意长度的整数BigDecimal
可以表示任意精度的浮点数
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。 -
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。import java.math.BigInteger; public class Test{ public static void main(String []args){ //long bigNum = 123456789123456789123456789L; BigInteger b1 = new BigInteger("12345678912345678912345678"); BigInteger b2 = new BigInteger("78923456789123456789123456789"); //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)); } } 和:78935802468035802468035802467 减:-78911111110211111110211111111 乘:974363656170906866147450004116994361840451151863907942 除:6392 余:9877181409877181409883013
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:四舍五入)
import java.math.BigDecimal;
public class Test{
public static void main(String []args){
BigInteger bi = new BigInteger("12433241123");
BigDecimal bd = new BigDecimal("12435.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));
System.out.println(bd.divide(bd2, 15, BigDecimal.ROUND_HALF_UP));
}
}
12433241123
1130.486
1130.486454545454545
java.util.Random
用于产生随机数
常用方法
`boolean nextBoolean()`:返回下一个伪随机数,它是取自此随机数生成器序列的均匀分布的 boolean 值。
`void nextBytes(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(包括)和指定值(不包括)之间均匀分布的 int 值。
`long nextLong()`:返回下一个伪随机数,它是取自此随机数生成器序列的均匀分布的 long 值。
import java.util.Random;
public class Test{
public static void main(String []args){
Random r = new Random();
System.out.println("随机整数:" + r.nextInt());
System.out.println("随机小数:" + r.nextDouble());
System.out.println("随机布尔值:" + r.nextBoolean());
}
}
随机整数:982802707
随机小数:0.47718683101721615
随机布尔值:false