Java常用类:数字相关类

本文深入解析Java中的数字类,包括整数Short、Int、Long,浮点数Float、Double,大整数BigInteger,大浮点数BigDecimal,以及随机数生成和数学运算工具类。详细介绍了各类的范围、默认值及使用示例。

Java常用类:数字相关类

大概率一览

整数 Short, Int, Long浮点数Float, Double
short, 16位, 2个字节,有符号的以二进制补码表示的整数 (-32768~32767, -2^15 ~2^15-1),默认值0float,单精度, 32位, 4个字节,符合IEEE 754标准的浮点数,默认值0.0f。 float的范围为1.40129846432481707e-45 to3.40282346638528860e+38 (无论正负)
int, 32位, 4个字节,有符号的以二进制补码表示的整数 (-2147483648~2147483647, -2^31 ~ 2^31-1),默认值0double,双精度, 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),默认值0Lfloat和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);
		//short a2 = 32768;  error 越界		
		
		int b1 = 2147483647;
		System.out.println(b1);
		//int b2 = 2147483648; error 越界		
		
		long c1 = 1000000000000L;
		System.out.println(c1);
		
		long c2 = 2147483647;  //隐式做了从int变成long的操作
		System.out.println(c2);
		
		long c3 = 2147483648L; //去掉L将报错
		System.out.println(c3);
	}
}
输出:
32767
2147483647
1000000000000
2147483647
2147483648

浮点数Float, Double

public class Float {
	public static void main(String[] args) {
		float f1 = 1.23f;
		// float f2 = 1.23;  error, float赋值必须带f
		System.out.println(f1);
		System.out.println((double)f1); //转换到double
		
		double d1 = 4.56d;
		System.out.println(d1);
		
		double d2 = 4.5;  //double 可以省略末尾d
		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对象
		BigInteger b2 = new BigInteger("987654321"); // 声明BigInteger对象
		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));//equals比较作用
		int flag = b1.compareTo(b2);//比较b1和b2的值,返回-1,0,1
		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对象
		BigDecimal b2 = new BigDecimal("987654321.123456789"); // 声明BigDecimal对象
		System.out.println("b1: " + b1 +  ", b2:" + b2);
		//需要指定位数,防止无限循环,或者包含在try-catch中
		//保留10位数,进行截断,BigDecimal.ROUND_HALF_UP四舍五入
		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类 随机生成在int范围内的随机数
		Random rd = new Random();
		System.out.println(rd.nextBoolean());
		System.out.println(rd.nextInt());
		System.out.println(rd.nextInt(100)); //0--100的随机数
		System.out.println(rd.nextLong());
		System.out.println(rd.nextDouble());		
		System.out.println("=========================");
		
		//第二种,生成一个范围内的随机数 例如0到时10之间的随机数
		//Math.random[0,1)
		System.out.println(Math.round(Math.random()*10));
		System.out.println("=========================");
		
		//JDK 8 新增方法
        rd.ints();  //返回无限个int类型范围内的数据
        int[] arr = rd.ints(10).toArray();  //生成10个int范围类的个数。(获取随机数的数组)
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
        System.out.println("=========================");
		
        arr = rd.ints(5,10 , 100).toArray();//产生10~100的五个数
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
        
        System.out.println("=========================");
        
        arr = rd.ints(10).limit(5).toArray();//本来要返回10个随机数,可以通过limit限制返回的个数
        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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值