JAVA API (application programming interface)

这篇博客详细介绍了JAVA API,包括异常处理的分类、捕获机制、自定义异常,以及Math类、String与StringBuilder、包装类、Object类的使用。此外,还深入探讨了集合类的分类、常用方法,如ArrayList、HashSet、TreeSet等,以及多线程的创建、状态、同步与锁机制。最后,提到了JDK8+的新特性和JAVA AWT组件的基础知识。
摘要由CSDN通过智能技术生成

第一章 概述

api 分成以下几个包讲解 :

1.java.lang (异常类String , Math , StringBuffer,StringBuilder , 包装类 ,Object ,Class 反射 )

2.java.util (日期类,日期转换 ,工具类,BigDecimal , 集合类…)

3.java.io (文件类 文件读写类…)

4.java.net(了解)

5.多线程(lang 重点)

6.java.awt(了解)

7.jdk8+ api (了解API的特性 Stream流)

第二章 异常类

什么是异常?

程序编译或运行当中,出现的问题,统称为异常。异常分为: 程序异常Exception和错误Error。

异常根类: Throwable

​ 程序异常类: Exception 由于程序员疏忽,设计不合理导致的问题,需要解决

​ 程序错误类: Error 程序中出现致命问题如 虚拟机崩溃,内存溢出,堆栈溢出… 不需要在程序中解决

Exception的分类

1.编译时异常: 在程序编译时发生了异常,如果不解决,无法编译

例如: IOException (IO 异常) SQLException(数据库异常) FileNotFoundException(文件未找到异常) …

2.运行时异常 : 编译时没有问题,运行时抛出异常(RuntimeException)
例如:

异常类 含义 示例
ArrayIndexOutofBoundsException 数组下标越界异常 int[] a = new int[4];
System.out.println(a[4]);
NullPointerException 空引用异常 String str = null;
System.out.println(str.length());
ArithmeticException 数学异常 10 / 0
ClassCastException 类型转换异常 Son s = (Son)new Father();
NumberFormatException 数字转换异常 Integer.parseInt(“ys”)
public class TestException1 {
   
	public static void main(String[] args) {
   
        //数学异常
		//System.out.println(10 / 0);
		//数组越界
//		int[] a = new int[4];
//		System.out.println(a[4]);
		//空引用
//		String str = null;		
//		System.out.println(str.length());
		//类型转换
//		Son s = (Son)new Father();
		//数字转换
		System.out.println(Integer.parseInt("ys") + 10);
	}
}
出现异常后的反应

1.系统打印错误信息

2.程序终止,突然中断

3.分配对象信息资源不变,可能资源泄露

总结:异常在程序中亟待解决的

异常捕获机制

异常的处理: 异常的捕获机制 (try catch finally

语法规则:

//监视器
try{
   
    //代码监视器,有可能发生异常的代码块
}catch(异常类型  变量名){
   
    //异常捕获器,处理问题    
}catch(异常类型  变量名){
   
    //异常捕获器    
} //...
finally{
   
    //清理块,无条件执行的语句块
}

注意:

1.try 监视器,不可以单独出现,需要配合catch(可以处理异常)或finally(不会处理异常)

2.catch可以有,捕获不同类型的异常,类似多条分支结果,n选1个,类型不要重复

3.因此将catch(Exception e) {} 放到catch的最后

4.finally 可以写也可以不写,无条件执行,除非退出虚拟机,包括return

public class SystemCode {
   
	
	public static void main(String[] args) {
   
		System.out.println("以下张雨代码....");
		try {
   
			System.out.println("..........");
			System.out.println(10 / 0);
		}
		catch(ArithmeticException e) {
   
			System.out.println("你发生了数学错误,赶紧改正");
			//return;
            //退出虚拟机
			System.exit(0);
		}
		catch(Exception e) {
   
			//保底
		}
		finally {
   
			System.out.println("无条件执行");
		}
		System.out.println("张雨代码结束....");
		
		
		System.out.println("以下义民代码....");
		
		System.out.println("..........");
				
		System.out.println("义民代码结束....");
	}
}

运行顺序:
在这里插入图片描述
情况1:

	try {
   
			System.out.println(10 / 0);
		} 
		catch (Exception e) {
   
			
			System.out.println("数学异常");
		}
		finally {
   
			System.out.println("执行finally");
		}
		
		System.out.println("执行该语句");
		
/**
数学异常
执行finally
执行该语句
*/

情况2:

	try {
   
			System.out.println(10 / 10);
		} 
		catch (Exception e) {
   
			
			System.out.println("数学异常");
		}
		finally {
   
			System.out.println("执行finally");
		}
		
		System.out.println("执行该语句");
/**
1
执行finally
执行该语句
*/

情况3:

		try {
   
			System.out.println(10 / 0);
		} 
		finally {
   
			System.out.println("执行finally");
		}
		
		System.out.println("执行该语句");

/**
执行finally
*/

情况4:

	try {
   
			System.out.println(10 / 0);
		} 
		catch (Exception e) {
   
			
			System.out.println("数学异常");
			return;
		}
		finally {
   
			System.out.println("执行finally");
		}
		
		System.out.println("执行该语句");

/**
数学异常
执行finally
*/

注意: 先执行finally ,再执行return

情况5:

	try {
   
			System.out.println(10 / 0);
		} 
		catch (Exception e) {
   
			
			System.out.println("数学异常");
			System.exit(0);
		}
		finally {
   
			System.out.println("执行finally");
		}
		
		System.out.println("执行该语句");

/**
数学异常
*/

异常的抛出

throw和throws

throws

方法内有异常发生,不解决,难以解决时,将方法内的异常向外抛出,使用throws。

语法:

控制权限  返回值  方法名(参数类型 参数变量) throws 异常类型1, 异常类型2... {
   
    
    //有异常
    
}

注意:

  1. 方法的异常抛给方法的调用者,调用者必须解决或继续抛,直到主方法,主方法抛给虚拟机
public class TestThrows {
   
	//方法向外抛出异常
	public static void test1() throws NullPointerException,ArithmeticException{
   		
		System.out.println(10 / 0);
	}
	//调用后不解决,继续抛
	public static void test2() throws Exception{
   		
		test1();
	}
	public static void main(String[] args) {
   
		try {
   
			//调用者
			test1();
		} catch (Exception e) {
   
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
  1. 方法重写时,子类不允许抛出更大类型异常
public class A {
   
	public void test() throws NullPointerException{
   
		
	}
}

class B extends A{
   
	//重写不能抛出更大类型
//	public void test() throws Exception{
   
//		
//	}
    //重载不限制
	public void test(int a) throws Exception{
   
		
	}
}
  1. 一般都是编译时异常抛出
  2. throws 抛出异常类
throw

当程序员需要手动创造一个异常,并且抛出去。自己构建一个异常。

语法:

//1 创建一个异常对象
Exception 对象名  = new Exception("异常的问题");
//2 throw抛出
throw 对象名;

例子:

public class TestThrow {
   
	public static void main(String[] args) {
   
		//使用检测年龄方法
		checkAge(9);
		
		System.out.println("继续编码代码");
	}
	
	/**
	 * 检测年龄是否合法
	 */
	public static void checkAge(int age) {
   
		if(age > 0 && age < 150) {
   
			System.out.println("年龄合法");
		
		}else {
   
			//构建异常
			RuntimeException e = new RuntimeException("年龄不合法,不能超出(1-150)");
			//手动抛出异常
			throw e;
		}
	}
}

throws 和 throw 区别?

throws throw
方法向外自动抛出异常 手动创建异常抛出
异常类 异常对象
方法后面 方法里

自定义异常

程序员自己构建一个异常类型,需要继承异常父类: RuntimeException或者Exception。

public class AgeException extends Exception{
   

	/**
	 * 序列号
	 */
	private static final long serialVersionUID = 1L;

	public AgeException() {
   
		super();
		// TODO Auto-generated constructor stub
	}

	public AgeException(String message) {
   
		super(message);
		// TODO Auto-generated constructor stub
	}
	
}

/**
	 * 检测年龄是否合法
	 * @throws AgeException 
	 */
	public static void checkAge(int age) throws AgeException {
   
		if(age > 0 && age < 150) {
   
			System.out.println("年龄合法");
		
		}else {
   			
			throw new AgeException("年龄非法");
		}
	}

第三章 Math

Math 类包含用于执行基本数学运算的方法。

注意:

  1. Math是final修饰的类,并不能拓展子类
  2. Math构造方法是私有的,所有属性和方法全是静态的 Math.属性 Math.方法

构造:构造被私有化

属性:

属性
PI 圆周率 public static final double
E 自然底数 public static final double

方法:

方法 含义 返回值或参数
random() 随机数 [a,b] : (int)((b - a + 1) * Math.random() + a)
ceil(double) 向上取整 获得比当前参数稍微大的整数,如果就是整数返回自己
floor(double) 向下取整 获得比当前参数稍微小的整数,如果就是整数返回自己
round(double) 四舍五入 返回四舍五入结果(整数位),注意:Math.floor(a + 0.5f)
sqrt(double) 开平方根 NaN : not a number
pow(a,b) a的b次幂 b个a相乘

案例1:随机生产 四个大写字母(验证码)

char [] code = new char[4];
		for (int i = 0; i < code.length; i++) {
   
			code[i] = (char)((90 - 65 + 1) * Math.random() + 65);
			for (int j = 0; j < i; j++) {
   
				if(code[j] == code[i]) {
   
					i --;
					break;
				}
			}
	
		}
System.out.println(Arrays.toString(code));

案例2: 猜数游戏

public class TestMath{
   
	public static void main(String[] args) {
   
		int num = (int) ((100 - 50 + 1) * Math.random() + 50);
		Scanner sc = new Scanner(System.in);
		int s = 50, e = 100;
		for (int i = 0; i < 5; i++) {
   
			System.out.println("第" + (i + 1) + "次机会请输入你猜的数字(" + s + "-" + e + ")");
			int m = sc.nextInt();
			if (num > m) {
   
				System.out.println("猜小了");
				s = m;
			} else if (num < m) {
   
				System.out.println("猜大了");
				e = m ;
			} else {
   
				System.out.println("Win");
				return;
			}
		}
		System.out.println("机会用完了,数字是:" + num);
	}
}

取整和四舍五入的案例:

	//向上取整
		System.out.println(Math.ceil(12.5));//13.0
		System.out.println(Math.ceil(12));//12.0
		System.out.println(Math.ceil(-8.01));//-8.0
		System.out.println(Math.ceil(-7.92)); // -7.0
		System.out.println("-----------------");
		//向下取整
		System.out.println(Math.floor(12.98)); //12.0
		System.out.println(Math.floor(-12.98));//-13.0
		System.out.println(Math.floor(14));//14.0
		//Math.floor(a + 0.5f) 
		//四舍五入
		System.out.println(Math.round(12.67));  //13
		System.out.println(Math.round(-12.67)); // -13
		System.out.println(Math.round(12.27));  //12
		System.out.println(Math.round(2.5));//3
		//特殊的
		System.out.println(Math.round(-2.5));//-2

开方和次幂:

		//开平方
		System.out.println(Math.sqrt(25));
		System.out.println(Math.sqrt(-36));//NaN	
		//求次幂
		System.out.println(Math.pow(3, 4));// 3 * 3 * 3 * 3

第四章 String和StringBuilder,StringBuffer

String

不可变字符串,一经定义不能改变。 " "括起来的,原样的。

注意: 字符串对象和字符串常量"abc" 都认为是对象型

String s = "abc123中";
		//字符串常量也是对象
		System.out.println(s.length());
		System.out.println("abcd".length());

构造方法:

new String();
new String("abc");
new String(StringBuilder sbl); //将可变字符串Builder转为String
//一共创建三个对象
String s1 = new String("abc");
String s2 = new String("abc");
		
String s3 = "abc";
String s4 = "abc";
System.out.println(s1 == s2);//false
System.out.println(s3 == s4);//true

在这里插入图片描述

使用 方法名 含义 返回值
长度 字符串.length() 字符串长度,汉字按照1个 返回字符个数
查找单个 字符串.charAt(下标) 字符串有下标,从0,对应下标的一个字符 一个字符
截取 字符串.substring(下标) 从下标截取到最后 截取的新的子串
截取区间 字符串.substring(起始下标,结束下标) 从起始下标截取到结束下标-1 同上
分割 字符串.split(切割符号) 字符串分割,注意特殊符号要加转义 字符串数组
查找子串 字符串.indexOf(子串) 从字符串中查找是否存在子串(左向右) 返回第一次下标,不存在返回-1
查找子串 字符串.lastIndexOf(子串) 从字符串中查找是否存在子串 返回最后一次下标,不存在返回-1
比较内容 字符串.equals(参数串) 参数串和字符串内容相同,不管地址 boolean
比较顺序 字符串.compareTo(参数串) 比较字典顺序 0 (相同)负的 正的
替换 字符串.replace(old,new) 将old替换成new 新串
正则校验 字符串.matches(正则) 匹配正则表达式
字符串.endsWith()
字符串.startsWith()
字符串.trim() 去掉两端空白

截取案例:

public class TestString2 {
   
	public static void main(String[] args) {
   
		String str = "abcd中国人";
		//长度
		//System.out.println("abcd中".length());

		//获得一个字符
		//System.out.println(str.charAt(4));		
		//System.out.println(str);
		
		//截取
		String s1 = str.substring(4);
		System.out.println(s1);	
		System.out.println(str.substring(0, 4));		
		System.out.println(str);
	}	
}

分割和查找案例:

public class TestString3 {
   
	public static void main(String[] args) {
   
		String city = "大连|本溪|丹东|辽阳";
		//分割
		String[] citys = city.split("\\|");
		
		for (String c : citys) {
   
			System.out.println(c);
		}
		
		String str = "abacdabddfgr";
		//字符串查找
		//第一次出现位置
		System.out.println(str.indexOf("ys"));
		//最后一次出现位置
		System.out.println(str.lastIndexOf("ab"));
	}
}

字符串比较:

public class TestString4 {
   
	public static void main(String[] args) {
   
		String str = "abcdabff";
		//字符串替换
		System.out.println(str.replace("ab", "*"));
		
		System.out.println(str);
		
		String s1 = "bdc";
		String s2 = new String("bdcd");
		//字符串比较内容
		//System.out.println(s1 == s2);//false
		//System.out.println(s1.equals(s2));//true
		
		//字符串比较顺序
		System.out.println(s1.compareTo(s2))<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值