Java中Math类:
Math类:和数***算相关:
如:初等函数,对数,平方根和三角函数
Math类的基本方法:
常量PI
Math.PI进行调用时java中的π值
	System.out.println(Math.PI);
求平方根sqrt()
		double x = 5;
		System.out.println(Math.sqrt(x));
求几次方pow(x,y) 求x的y次方
	double a =3.2;
	double b =2.7;
	System.out.println(Math.pow(a,b));
round(x) 进行四舍五入计算
ceil(x) 向上取整(往无穷大的方向取整)
floor(x) 向下取整(往无穷小的方向取整)
	@Test
	public void test() {
		System.out.println(Math.round(2.4));
		System.out.println(Math.ceil(2.4));//返回值为浮点数
		System.out.println(Math.floor(2.4));//返回值为浮点数
		System.out.println();
		//这个是特殊的
		System.out.println(Math.round(2.5));
		System.out.println(Math.round(-2.5));
		System.out.println();
		
		System.out.println(Math.round(2.6));
		System.out.println(Math.ceil(2.6));
		System.out.println(Math.floor(2.6));
		System.out.println();
		
		System.out.println(Math.max(0.5, 1));
	}
注意2.5和-2.5的round()的结果是特殊的需要特殊记忆,并不是真的四舍五入
max(x,y) 获取x和y中较大的那个
min(x,y) 获取x和y中较小的那个
	@Test
	public void test2() {
		System.out.println(Math.max(5, 3));
		System.out.println(Math.min(5, 3));
		int a = 10;
		double b = 5.2;
		System.out.println(Math.max(a,b));
		System.out.println(Math.min(a,b));
	}
random()生成随机数 范围是[0,1)
	@Test
	public void test3() {
		System.out.println(Math.random());
	}
java.util.Random类 是专门来产生随机数的
 1.double nextdouble() 0.0-1.0
 2.int nextint() 0-1
 3.int nextint(int x) 产生0-x之间的随机数
import java.lang.Random;
	@Test
	public void test4() {
		Random m = new Random();
		System.out.println(m.nextInt());
		System.out.println(m.nextDouble());
		System.out.println(m.nextInt(10));
	}
Math类的BigInteger类和BigDecimal类
1. BigInteger和BigDecimal
public class TestMath {
	@Test
	public void tset() {
		BigInteger big1 = new BigInteger("1234567890123456789");
		BigInteger big2 = new BigInteger("1234567890123456789");
		
		System.out.println("两个大整数的和为"+big1.add(big2));
		System.out.println("两个大整数的差为"+big1.subtract(big2));
		System.out.println("两个大整数的积为"+big1.multiply(big2));
		System.out.println("两个大整数的商为"+big1.divide(big2));
		System.out.println("两个大整数的幂为"+big1.pow(3));	
	}	
	@Test
	public void tset2() {
		BigDecimal big1 = new BigDecimal("1234.567890123456789");
		BigDecimal big2 = new BigDecimal("123456789012.3456789");
		
		System.out.println("两个大浮点的和为"+big1.add(big2));
		System.out.println("两个大浮点的差为"+big1.subtract(big2));
		System.out.println("两个大浮点的积为"+big1.multiply(big2));
		//两个大浮点数做除法,用这哥方法如果没办法整除的话会报异常
		
		//System.out.println("两个大浮点的商为"+big1.divide(big2));
		System.out.println("两个大浮点数做除法:"+big1.divide(big2, 6, BigDecimal.ROUND_CEILING));
		System.out.println("两个大浮点的幂为"+big1.pow(3));	
	}
	
	
}
2.常见面试题
(1)面试题:int和Integer和BigInteger的区别是什么?
	int:是基本数据类型
	Integer:是包装类
	BigInteger:是不可变的任意精度的整数(对象)
(2)double Double 和BigDecimal的区别?
	double:基本数据类型
	Double:是double的包装类
	BigDecimal;是不可变的任意精度的十进制浮点数(对象)
(3)关于基本数据类型,包装数据类型和大整数(大浮点数)类型计算方面
	1.基本数据类型,之间进行+- * /
	2.包装数据类型:通过拆箱操作然后再计算
	3.BigInteger,BigDecimal:通过方法

 京公网安备 11010502036488号
京公网安备 11010502036488号