Java学习-第15天


		// 必须传入字符串
		BigInteger v1 = new BigInteger("123123");
		BigDecimal v2 = new BigDecimal(20);
		BigDecimal v3 = new BigDecimal(20);
		// + 
		BigDecimal result = v2.add(v3);
		System.out.println(result);
		// - 
		result = v2.subtract(v3);
		System.out.println(result);
		// * 
		result = v2.multiply(v3);
		System.out.println(result);
		// / 
		result = v2.divide(v3);
		System.out.println(result);
		// % 
		result = v2.remainder(v3);
		System.out.println(result);
	

	public static void main(String[] args) {
		System.out.println(test(120));
		System.out.println(test1(120));
		System.out.println(Long.MAX_VALUE);
	}
	public static long test(long n){
		long result = 1;
		for (int i = 1; i <= n; i++) {
			result *= i;
		}
		return result;
	}
	
	public static BigInteger test1(long n){
		BigInteger result = new BigInteger("1");
		for (int i = 1; i <= n; i++) {
			result= result.multiply(new BigInteger(i+""));
		}
		return result;
	}

		// 绝对值
		System.out.println(Math.abs(-3));
		// 向上取整 , 有小数 就进位
		System.out.println(Math.ceil(2.000000001));
		// 向下取整 , 舍弃小数
		System.out.println(Math.floor(2.999999999999));
		// 返回最大的值
		System.out.println(Math.max(2.3, 4.1));
		// 返回最小的值
		System.out.println(Math.min(2.3, 4.1));
		// 开平方
		System.out.println(Math.sqrt(16));
		// 开立方
		System.out.println(Math.cbrt(8));
		// 返回一个 大于等于0 且小于 1 的随机数
		// 本质就是Random的nextDouble方法
		System.out.println(Math.random());
		// 四舍六入五取偶 , 当小数大于0.5  就进1 , 小于 0.5 就舍弃,当小数为0.5整的时候,取偶数
		// 2.5  = 2 , 3.5 = 4 , 2.50001 = 3 
		System.out.println(Math.rint(2.50001));
		System.out.println(Math.rint(9.5));
		// N的M次幂 ,  2的3次幂
		System.out.println(Math.pow(2, 3));
	

	public static void main(String[] args) {
		// 1 > x >= 0
		// 123 > x >= 97
		System.out.println(Math.random());
		// Math.random() * (最大-最小+1) + 最小
		char result = (char) (Math.random() * (122 - 97 + 1) + 97);
		System.out.println(result);
	}

 * 异常 其实就是错误的一种说法,在java中 有一个专门模拟所有异常和错误的类 Throwable ,是所有异常类的父类
 * 
 * try...catch... : 处理异常,一般用于客户端
 * 
 * throws : 抛出异常,一般用于服务端
 * 
 * throw : 异常源点
 * 
 * finally : 必须执行的语句

	public static void main(String[] args) {
		System.out.println("------------");
		// 除数不能为0
		int a = 10;
		int b = 0;
		if (b != 0) {
			int c = a / b;
		} else {
			System.out.println("除数不能为0");
		}

		System.out.println("===========");
	}
 * try{
 * 		高风险代码
 * 		只要出错,就执行catch,try中剩余代码不再执行
 * 		如果没出错,try就可以顺利执行完成,并且catch不再执行
 * }catch(异常类型 变量){
 * 		处理方案;
 * }

	public static void main(String[] args) {
		try {
			// 加载对应的文件
			FileInputStream fis = new FileInputStream("123.txt");
			System.out.println("=========");
		} catch (Exception e) {
			// 打印错误栈帧和信息,一般用于程序员排错
			e.printStackTrace();
			// 获取错误信息,一般用于反馈给客户端
			String msg = e.getMessage();
			System.out.println(msg);
			System.out.println("xxxxxxxxxx");
		}
		// 不再终止生命周期执行
		System.out.println(1111);
	}
 * throws 抛出异常,该方式并不会处理理解,一种提醒机制,告诉调用人员,这里可能会有错误
 * 
 * 如果 知道如何解决 则使用try 解决问题, 否则就要通过 throws 抛出问题
 * 
 * 只要你的下家提醒了你,要么你解决,要么抛给上家

	public static void main(String[] args) {
		try {
			m1();
		} catch (Exception e) {
			// e.printStackTrace();
			// TODO 解决错误的措施
		}
		System.out.println("====");
	}

	// throws 可以同时抛出多个异常,多个用逗号隔开
	public static void m1() throws FileNotFoundException,Exception {
		m2();
	}

	public static void m2() throws FileNotFoundException {
		m3();
	}

	public static void m3() throws FileNotFoundException {
		new FileInputStream("123");
	}
 * try{
 * 		高风险代码;
 * }catch(异常类型 变量){
 * 		处理措施;
 * }catch(异常类型 变量){
 * 		处理措施;
 * }catch(异常类型 变量){
 * 		处理措施;
 * }..... 
 * 
 * 可以写多个catch  , 但是 从上往下 必须是从子类到父类,或者没有继承关系
 * 

	public static void main(String[] args) {
		// 当try下面 可能会出现多个异常的时候,并且每个异常对应的解决方案不同的时候
		// 需要写多个catch进行不同的处理
		// 如果解决方案一致,则只写一个 exception 即可
		try {
			new FileInputStream("123");
			String stre = null;
			System.out.println(stre.trim());
		} catch (FileNotFoundException e) {
			System.out.println("找不到指定文件");
		} catch (IOException e) {
		} catch (NullPointerException e) {
			System.out.println("不能为空");
		} catch (Exception e) {
			System.out.println("其他异常");
		}
	}
 * try{
 * 
 * }catch(异常类型1 | 异常类型2 | 异常类型3 ...){
 * 
 * }
 * 
 * 同时捕捉多个异常,并且多个异常类型直接用 | 隔开,并且 不能有继承关系
 * 如果有继承关系 则直接写父类即可

	public static void main(String[] args) {
		try {
			new FileInputStream("123");
		} catch ( FileNotFoundException | NullPointerException e) {
			// TODO: handle exception
		}
	}
 * finally : 必须执行的语句块,不能单独使用,必须和try 或者try..catch..一起使用
 * 
 * finally 只有一种不执行的情况,那就是 System.exit(0);

	public static void main(String[] args) {
		try {
			FileInputStream fis = new FileInputStream("123");
			System.out.println(fis);
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			System.out.println("必须执行");
		}
	}

	public static void main(String[] args) {
		int result = m1();
		// 11
		System.out.println(result);
	}

	public static int m1() {
		int i = 10;
		try {
			// 因为finally中有return,所以这里的return 不执行,但是 i++执行,编译之后 会把return去掉
			return i++;
		} catch (Exception e) {
		} finally {
			System.out.println(i + "-------");
			return i;
		}
	}

	public static void main(String[] args) {
		// 提升作用域,否则访问不到
		// 添加默认值,否则因为局部变量没有默认值而报错
		FileInputStream fis = null;
		try {
			fis = new FileInputStream("D://123.txt");
			System.out.println("执行成功");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				// 判断是否打开该资源
				if (fis != null) {
					System.out.println("关闭成功");
					fis.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
 * java7新特征,自动关闭资源
 * 
 * try(开启资源语句;){
 * 		高风险代码;
 * }catch(异常类型 变量){
 * 		处理方案;
 * }
 * 
	public static void main(String[] args) {
		try(
				FileInputStream fis = new FileInputStream("123");
				) {
			// 操作
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
 * 方法覆写,不能比原方法有更宽泛的异常
 * 
 * 子类方法抛出的异常,不能大于父类异常,也就是 小于等于 父类异常均可
 * 			要么和父类抛出的异常一public class Exception_10 {
	public static void main(String[] args) {

	}
}
class A {
	public void m1() throws IOException {

	}
}
class B extends A {
	public void m1() throws IOException {

	}
}致,要么是父类抛出异常类的子类,或者 直接不抛出异常

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值