Java笔记(基本数据类型的封装类、内部类、异常处理)

本文介绍了Java中的基本数据类型的封装类及其常用方法,如转换为二进制、十六进制和八进制。接着讲解了内部类的概念,包括如何创建和使用内部类对象。最后,详细阐述了Java中的异常处理机制,包括运行时异常的捕获、多异常处理以及finally块的使用,并举例说明了受检查异常的处理方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 * 基本数据类型的封装类
 * 内部类
 * 异常处理
 */



1.基本数据类型的封装类(带方法的数据类型)
	
	基本数据类型:byte, short, int, long, float, double, char, boolean
	对应的封装类:Byte, Short, Integer, Long, Float, Double, Character, Boolean

	System.out.println(Integer.toBinaryString(10));		//转 2 进制
	System.out.println(Integer.toHexString(10));		//转 16 进制
	System.out.println(Integer.toOctalString(10));		//转 8 进制

	//求100的阶层
	// BigInteger(比较大的整数)	BigDecimal(比较大的小数)
	BigDecimal bigDecimal = new BigDecimal(1);
		
	for(int i = 2; i <= 100; i++) {
		//自身(bigDecimal)乘以 new BigDecimal(i)
		bigDecimal = bigDecimal.multiply(new BigDecimal(i));
	}
	System.out.println(bigDecimal);

2.内部类(类里面才声明一个类)
	public class Animal {

		public void out() {
			System.out.println("我是外部类的一个方法");
		}
	
		//内部类(默认 public 修饰)
		class Fly {
			public void in() {
				System.out.println("我是内部类的一个方法");
			}
		}
	}
	//怎么得到内部类的对象呢?
	//先实例化外部类,再实例化内部类

	//第一种写法
	Animal.Fly fly = new Animal().new Fly();
	//不能使用外部类的方法,只能使用内部类的方法
	fly.in();

	//第二种写法
	Animal a = new Animal();
	Animal.Fly fly2 =  a.new Fly();
	fly2.in();

3.异常处理
	
	3.1 运行时异常(用户操作问题产生,可以避免)

		//java.lang.ArithmeticException: / by zero	算数异常,除数为0
		int a = 12;
		int b = 0;
		System.out.println(a/b);	

		//java.lang.ArrayIndexOutOfBoundsException: 3	数组下标越界
		int[] arr = {1, 2, 3};
		System.out.println(arr[3]);

		//上面的语句出现了异常Exception(不是错误Error)
		//我们可以捕捉异常
		try {
			int[] arr = {1, 2, 3};
			System.out.println(arr[3]);		//有可能出现异常的代码
		} catch (ArrayIndexOutOfBoundsException e) {	//异常是否为 catch() 里面的
			System.out.println("数组下标越界了");	//捕捉后进行的操作
			e.printStackTrace();			//把异常在控制台中打印出来
		}

		//上面只能捕捉一种异常,那要是出现了多个异常呢?
		try {
			int a = 12;
			int b = 0;
			int[] arr = {1, 2, 3};
		
			System.out.println(a/b);
			System.out.println(arr[3]);
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("除数不能为0");
		}catch (ArithmeticException e) {
			System.out.println("数组下标越界了");
		}

		//Exception 是所有异常的父类
		try {
			int a = 12;
			int b = 0;
			int[] arr = {1, 2, 3};
						//这里有两个异常
			System.out.println(a/b);
			System.out.println(arr[3]);
		} catch (Exception e) {		//不管几个异常,用一个 Exception 就能解决
			System.out.println("出现了异常");	
		}
	
		//如果不捕捉异常,出现了异常或异常不匹配,后面的代码就没办法执行了

		//不管出没出现异常,我都要执行这个代码块,该怎么做?	finally
		try {
			int[] arr = {1, 2, 3};
			System.out.println(arr[3]);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			System.out.println("不管有没有异常,我都要执行");
		}

		//如果 try 里面有 return, 那么先执行 finally 里面的代码块,再执行 return

	3.2 受检查异常(由程序设计原因造成,强制进行处理)
		
	try..catch 是第一种处理异常的机制

	throws 是第二种处理异常的机制,使用throws将异常向上抛出,谁调用方法谁处理异常
		public void show() throws 异常类型1,异常类型2{}
		public void show() throws Exception{}
	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值