Java常用类:数字相关类
大概率一览
| 整数 Short, Int, Long | 浮点数Float, Double |
|---|
| short, 16位, 2个字节,有符号的以二进制补码表示的整数 (-32768~32767, -2^15 ~2^15-1),默认值0 | float,单精度, 32位, 4个字节,符合IEEE 754标准的浮点数,默认值0.0f。 float的范围为1.40129846432481707e-45 to3.40282346638528860e+38 (无论正负) |
| int, 32位, 4个字节,有符号的以二进制补码表示的整数 (-2147483648~2147483647, -2^31 ~ 2^31-1),默认值0 | double,双精度, 32位, 4个字节,符合IEEE 754标准的浮点数,默认值0.0d。 double的范围为4.94065645841246544e-324d to 1.79769313486231570e+308d (无论正负) |
| long, 64位, 8个字节,有符号的以二进制补码表示的整数(-9,223,372,036,854,775,808~9,223,372,036,854,775,807)(-2^63 ~2^63 -1),默认值0L | float和double都不能用来表示很精确的数字,要表示比较精确的数可以用BigInteger(大整数)/BigDecimal(大浮点数) |
| BigInteger(大整数) | BigDecimal(大浮点数) |
|---|
| 支持无限大的整数运算 | 支持无限大的小数运算,注意精度和截断 |
| 随机数类 Random | 工具类 Math |
|---|
| nextInt() 返回一个随机int | 对数函数log(以e=2.718为底) |
| nextInt(int a) 返回一个[0,a)之间的随机int | 绝对值函数abs |
| nextDouble()返回一个[0.0,1.0]之间double | 四舍五入函数round等 |
| ints 方法批量返回随机数数组 | 幂函数pow |
| Math.random() 返回一个[0.0,1.0]之间double | 向下取整floor;向上取整ceil |
| 取最大值max;取最小值min |
整数Short, Int, Long
public class Int {
public static void main(String[] args) {
short a1 = 32767;
System.out.println(a1);
int b1 = 2147483647;
System.out.println(b1);
long c1 = 1000000000000L;
System.out.println(c1);
long c2 = 2147483647;
System.out.println(c2);
long c3 = 2147483648L;
System.out.println(c3);
}
}
输出:
32767
2147483647
1000000000000
2147483647
2147483648
浮点数Float, Double
public class Float {
public static void main(String[] args) {
float f1 = 1.23f;
System.out.println(f1);
System.out.println((double)f1);
double d1 = 4.56d;
System.out.println(d1);
double d2 = 4.5;
System.out.println((float)d2);
}
}
输出:
1.23
1.2300000190734863
4.56
4.5
大整数BigInteger
| 方法 | 内容 |
|---|
| add | 加法操作 |
| subtract | 减法操作 |
| multiply | 乘法操作 |
| divide | 除法操作 |
| max/min | 求出最大/最小数 |
| divideAndRemainder | 求出余数的除法操作,返回值是一个具有两个值的数组,第一个值是商,第二个值是余数 |
| equals | 判断是否相等,返回值为Boolean |
| compareTo | 比较作用,返回值为-1,0,1 |
| abs | 绝对值 |
| pow | 幂次 |
import java.math.BigInteger;
public class BigIntegerTest {
public static void main(String[] args) {
BigInteger b1 = new BigInteger("123456789");
BigInteger b2 = new BigInteger("987654321");
System.out.println("b1: " + b1 + ", b2:" + b2);
System.out.println("加法操作:" + b2.add(b1));
System.out.println("减法操作:" + b2.subtract(b1));
System.out.println("乘法操作:" + b2.multiply(b1));
System.out.println("除法操作:" + b2.divide(b1));
System.out.println("最大数:" + b2.max(b1));
System.out.println("最小数:" + b2.min(b1));
BigInteger result[] = b2.divideAndRemainder(b1);
System.out.println("商是:" + result[0] + ";余数是:" + result[1]);
System.out.println("等价性是:" + b1.equals(b2));
int flag = b1.compareTo(b2);
if (flag == -1)
System.out.println("比较操作: b1<b2");
else if (flag == 0)
System.out.println("比较操作: b1==b2");
else
System.out.println("比较操作: b1>b2");
BigInteger b3=new BigInteger("-123");
System.out.println("绝对值:"+b3.abs());
System.out.println("幂次:"+b3.pow(3));
}
}
输出:
b1: 123456789, b2:987654321
加法操作:1111111110
减法操作:864197532
乘法操作:121932631112635269
除法操作:8
最大数:987654321
最小数:123456789
商是:8;余数是:9
等价性是:false
比较操作: b1<b2
绝对值:123
幂次:-1860867
大浮点数BigDecimal
大部分方法与大整数BigInteger相同,但有除法略有不同。
| 特别方法 | 内容 |
|---|
| b2.divide(b1,10,BigDecimal.ROUND_HALF_UP) | 除法操作要注意精度和截断,不然会陷入无限循环报错 ,表b2除b1,且保留10位小数,(BigDecimal.ROUND_HALF_UP)表保留方式是四舍五入 |
import java.math.BigDecimal;
public class zxc {
public static void main(String[] args) {
BigDecimal b1 = new BigDecimal("123456789.987654321");
BigDecimal b2 = new BigDecimal("987654321.123456789");
System.out.println("b1: " + b1 + ", b2:" + b2);
System.out.println("除法操作:" + b2.divide(b1,10,BigDecimal.ROUND_HALF_UP));
}
}
输出:
b1: 123456789.987654321, b2:987654321.123456789
除法操作:8.0000000099
随机数类 Random
| 方法 | 内容 |
|---|
| nextInt() | 返回一个随机的int |
| nextInt(int a) | 返回一个[0,a)之间的随机int |
| nextDouble() | 返回一个[0.0,1.0]之间的double |
| nextBoolean() | 返回一个随机boolean值 |
| ints | 批量返回随机数数组(JDK 8新增) |
| Math.random() | 返回一个[0.0,1.0]之间double |
import java.util.Random;
public class RandomTest {
public static void main(String[] args)
{
Random rd = new Random();
System.out.println(rd.nextBoolean());
System.out.println(rd.nextInt());
System.out.println(rd.nextInt(100));
System.out.println(rd.nextLong());
System.out.println(rd.nextDouble());
System.out.println("=========================");
System.out.println(Math.round(Math.random()*10));
System.out.println("=========================");
rd.ints();
int[] arr = rd.ints(10).toArray();
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
System.out.println("=========================");
arr = rd.ints(5,10 , 100).toArray();
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
System.out.println("=========================");
arr = rd.ints(10).limit(5).toArray();
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
输出:
true
-1011580266
19
-6338263010519028604
0.5669580282187722
=========================
3
=========================
-1756163615
568833444
930759453
32623942
551679634
1725831172
1724366309
-1764706658
-398931850
-1134052567
=========================
89
16
12
45
66
=========================
550693140
1740155463
151738705
1247114064
1206202955
工具类 Math
| 方法 | 内容 |
|---|
| Math.abs(a) | 求a绝对值 |
| Math.log(a) | 求a的对数,以e=2.718为底 |
| Math.log(n)/Math.log(m) | 求n的对数,以m为底(左边的式子中/是除法) |
| Math.max(m,n)/Math.min(m,n) | 求m,n两数中的最大值/最小值 |
| Math.pow(m,n) | 求幂m^n |
| Math.round() | 四舍五入 |
| Math.ceil() | 向上取整3.5->4.0 |
| Math.floor() | 向下取整3.5->3.0 |
public class MathTest {
public static void main(String[] args) {
System.out.println(Math.log(8));
System.out.println(Math.log(8)/Math.log(2));
System.out.println(Math.abs(-5));
System.out.println(Math.max(-5,-8));
System.out.println(Math.pow(-5,2));
System.out.println(Math.round(3.5));
System.out.println(Math.ceil(3.5));
System.out.println(Math.floor(3.5));
}
}
输出:
2.0794415416798357
3.0
5
-5
25.0
4
4.0
3.0