目录
3.String&StringBuilder&StringBuffer
(1)length & subString & charAt
(3)getBytes(字符串和byte数字间的相互转换 )&编码问题
1. 异常
(1)什么是异常?
(2)如何处理异常?
- try-catch-finally
- throw
- throws
- 自定义异常
- 异常链
1.1 什么是异常?
- 异常本质上是程序上的错误。
- 在程序运行过程中,意外发生的情况,背离程序本身意图的表现
(1)编译期间的异常
(2)运行期间的异常
1.2 异常的分类
1.3 异常处理机制
异常处理机制可分为抛出异常 & puzuo异常
1.4 try-catch-finally
异常案例:
public class TryDemoOne {
public static void main(String[] args) {
//要求:定义两个整数,接受用户的键盘输入,输出两数之商
Scanner sc = new Scanner(System.in);
int x1 = sc.nextInt();
int x2 = sc.nextInt();
System.out.println("result is " + x1/x2);
/* 正常结果
19
4
result is 4
*/
/* 异常1:除0
19
0
Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.vb.exception.TryDemoOne.main(TryDemoOne.java:11)
*/
/* 异常2: 出入格式错误异常
w
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at com.vb.exception.TryDemoOne.main(TryDemoOne.java:9)
*/
}
}
try-catch-finally处理案例:
public class TryDemoOne {
public static void main(String[] args) {
//要求:定义两个整数,接受用户的键盘输入,输出两数之商
Scanner sc = new Scanner(System.in);
try{
// 编写可能出现异常的代码 -->铺货异常
int x1 = sc.nextInt();
int x2 = sc.nextInt();
System.out.println("result is " + x1/x2);
}catch(Exception e){
// 打印错误的堆栈信息 -->抛出异常
e.printStackTrace();
System.out.println("程序出错了");
}finally {
// 无论catch中的内容执行与否,finally中的内容都会执行
System.out.println("运行结束");
}
/**
* 89
* 0
* java.lang.ArithmeticException: / by zero
* at com.vb.exception.TryDemoOne.main(TryDemoOne.java:41)
* 程序出错了
* 运行结束
*/
/**
* 90
* s
* java.util.InputMismatchException
* at java.util.Scanner.throwFor(Scanner.java:864)
* at java.util.Scanner.next(Scanner.java:1485)
* at java.util.Scanner.nextInt(Scanner.java:2117)
* at java.util.Scanner.nextInt(Scanner.java:2076)
* at com.vb.exception.TryDemoOne.main(TryDemoOne.java:40)
* 程序出错了
* 运行结束
*/
}
}
具体多类型异常抛出
// catch模块改进
try{
// 编写可能出现异常的代码 -->铺货异常
int x1 = sc.nextInt();
int x2 = sc.nextInt();
System.out.println("result is " + x1/x2);
}catch(ArithmeticException e){
// 打印错误的堆栈信息 -->抛出异常
System.out.println("Arithmetic exception,除数不允许为0!");
e.printStackTrace();
}
catch(InputMismatchException e){
// 打印错误的堆栈信息 -->抛出异常
System.out.println("请输入整数");
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
} finally {
// 无论catch中的内容执行与否,finally中的内容都会执行
System.out.println("运行结束");
}
/*
90
0
Arithmetic exception,除数不允许为0!
运行结束
java.lang.ArithmeticException: / by zero
at com.vb.exception.TryDemoOne.main(TryDemoOne.java:79)
*/
/*
90
e
请输入整数
运行结束
java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at com.vb.exception.TryDemoOne.main(TryDemoOne.java:78)
*/
}
System.exit();
try{
// 编写可能出现异常的代码 -->铺货异常
int x1 = sc.nextInt();
int x2 = sc.nextInt();
System.out.println("result is " + x1/x2);
}catch(ArithmeticException e){
// 打印错误的堆栈信息 -->抛出异常
System.exit(1); // 中止程序运行
//12
//0
System.out.println("Arithmetic exception,除数不允许为0!");
e.printStackTrace();
}
不建议finally中编写return
public class TryDemoTwo {
public static int test(){
Scanner sc = new Scanner(System.in);
try{
// 编写可能出现异常的代码 -->铺货异常
int x1 = sc.nextInt();
int x2 = sc.nextInt();
return x1/x2;
}catch(ArithmeticException e){
// 打印错误的堆栈信息 -->抛出异常
System.out.println("Arithmetic exception,除数不允许为0!");
return 0;
} finally {
// 无论catch中的内容执行与否,finally中的内容都会执行
System.out.println("运行结束");
return -100;
}
}
public static void main(String[] args) {
int result = test();
System.out.println("x1和x2的商为:"+ result); // 7
}
/*
不建议finally 中写return
*/
/*
23
5
运行结束
x1和x2的商为:-100
*/
/*
12
0
Arithmetic exception,除数不允许为0!
运行结束
x1和x2的商为:-100
*/
}
1.5 throw & throws
可以通过throws声明将要抛出何种类型的异常,通过throw将产生的异常抛出
throws:
如果一个方法可能会出现异常,但没有能力处理这种异常,可以在方法声明处调用throws子句来声明抛出异常。
1.5.1 throws
public class TryDemoThrows {
/*
通过throws 抛出异常时,正对可能出现的多种异常情况,解决方案
1. throws 后边接入多个异常类型
2. throws 后边直接写Exception
*/
/**
* 测试接受数据相除结果结果的方法
* @return 两个接受数据的商
* @throws ArithmeticException
* @throws InputMismatchException
*/
public static int test() throws ArithmeticException, InputMismatchException {
Scanner sc = new Scanner(System.in);
int x1 = sc.nextInt();
int x2 = sc.nextInt();
System.out.println("运行结束");
return x1/x2;
}
// public static int test() throws Exception {
// Scanner sc = new Scanner(System.in);
// int x1 = sc.nextInt();
// int x2 = sc.nextInt();
// System.out.println("运行结束");
// return x1/x2;
// }
public static void main(String[] args) {
try{
int result = test();
System.out.println("x1和x2的商为:"+ result);
}catch (ArithmeticException e){
System.out.println("除数不允许为0");
e.printStackTrace();
}catch (InputMismatchException e){
System.out.println("输入应为zhengshu");
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}
}
/*
re
输入应为zhengshu
java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at com.vb.exception.TryDemoThrows.test(TryDemoThrows.java:10)
at com.vb.exception.TryDemoThrows.main(TryDemoThrows.java:18)
*/
/*
45
0
运行结束
除数不允许为0
java.lang.ArithmeticException: / by zero
at com.vb.exception.TryDemoThrows.test(TryDemoThrows.java:13)
at com.vb.exception.TryDemoThrows.main(TryDemoThrows.java:18)
*/
}
1.5.2 throw
- throw用来抛出一个异常
- 例如: throw new IOException();
- throw抛出的只能够是可抛出Throwable 或者其子类的实例对象
- 例如: throw new String("出错了");错误编写。
- throw可以规避可能出现的风险,完成一些程序的逻辑
案例代码:
/**
* 需求: 酒店入住规则
* 限定年龄,18岁以下\80岁以上的住客必须有亲友陪同
*
* throw 抛出异常对象的处理方案
* 1. 通过try..catch 包含throw语句 -- 自己抛出,自己处理
* 2. 通过throws在方法声明处抛出异常 -- 谁调用谁处理
-- 调用者可以自己处理,可以继续向上抛出
2-2 可以抛出与throw对象相同的类型或者让父类
*/
方案一:通过try..catch 包含throw语句 -- 自己抛出,自己处理
public static void testAge() {
try {
System.out.println("请输入住客年龄");
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
if (age < 18 || age > 80) {
throw new Exception("18岁以下\\80岁以上的住客必须有亲友陪同");
} else {
System.out.println("可以入住");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
testAge();
/*
请输入住客年龄
89
java.lang.Exception: 18岁以下\80岁以上的住客必须有亲友陪同
at com.vb.exception.ThrowDemo.testAge(ThrowDemo.java:18)
at com.vb.exception.ThrowDemo.main(ThrowDemo.java:29)
*/
}
方案2: 通过throws在方法声明处抛出异常 -- 谁调用谁处理
// 方案2: 2. 通过throws在方法声明处抛出异常 -- 谁调用谁处理
public static void testAge() throws Exception{
System.out.println("请输入住客年龄");
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
if (age < 18 || age > 80) {
throw new Exception("18岁以下\\80岁以上的住客必须有亲友陪同");
} else {
System.out.println("可以入住");
}
}
public static void main(String[] args) {
try {
testAge();
} catch (Exception e) {
e.printStackTrace();
}
/*
请输入住客年龄
90
java.lang.Exception: 18岁以下\80岁以上的住客必须有亲友陪同
at com.vb.exception.ThrowDemo.testAge(ThrowDemo.java:41)
at com.vb.exception.ThrowDemo.main(ThrowDemo.java:63)
*/
}
方案2-2:throws: Throwable
//方案2-2
public static void testAge() throws Throwable{
System.out.println("请输入住客年龄");
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
if (age < 18 || age > 80) {
throw new Exception("18岁以下\\80岁以上的住客必须有亲友陪同");
} else {
System.out.println("可以入住");
}
}
public static void main(String[] args) {
try {
testAge();
// } catch (Exception e) {
} catch (Throwable e) {
e.printStackTrace();
}
/*
请输入住客年龄
90
java.lang.Exception: 18岁以下\80岁以上的住客必须有亲友陪同
at com.vb.exception.ThrowDemo.testAge(ThrowDemo.java:41)
at com.vb.exception.ThrowDemo.main(ThrowDemo.java:63)
*/
}
1.6 自定义异常
自定义异常类:
public class HotelAgeException extends Exception{
public HotelAgeException(){
super("18岁以下\\80岁以上的住客必须有亲友陪同");
}
}
测试类
public class HotelExceptionTest {
public static void testAge() throws HotelAgeException{
System.out.println("请输入住客年龄");
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
if (age < 18 || age > 80) {
throw new HotelAgeException();
} else {
System.out.println("可以入住");
}
}
public static void main(String[] args) {
try{
testAge();
}catch (HotelAgeException e){
System.out.println(e.getMessage());
System.out.println("酒店人员不能办理入住");
}catch (Exception e){
e.printStackTrace();
}
/*01
请输入住客年龄
23
可以入住
*/
/*02
请输入住客年龄
u
java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at com.vb.exception.HotelExceptionTest.testAge(HotelExceptionTest.java:10)
at com.vb.exception.HotelExceptionTest.main(HotelExceptionTest.java:20)
*/
/*03
请输入住客年龄
98
18岁以下\80岁以上的住客必须有亲友陪同
酒店人员不能办理入住
*/
}
}
1.7 异常链
public class ExceptionLine {
public static void main(String[] args){
try {
testThree();
} catch (Exception e) {
e.printStackTrace();
/* (正常的异常链中只保留了最后一个异常信息,
可以通过配置将所有的异常堆栈信息保留下来)
java.lang.Exception: testThree 新产生的异常
at com.vb.exception.ExceptionLine.testThree(ExceptionLine.java:29)
at com.vb.exception.ExceptionLine.main(ExceptionLine.java:7)
*/
/* 通过方式1&2保留完整的异常堆栈信息
java.lang.Exception: testThree 新产生的异常
at com.vb.exception.ExceptionLine.testThree(ExceptionLine.java:43)
at com.vb.exception.ExceptionLine.main(ExceptionLine.java:7)
Caused by: java.lang.Exception: testTwo 新产生的异常
at com.vb.exception.ExceptionLine.testTwo(ExceptionLine.java:31)
at com.vb.exception.ExceptionLine.testThree(ExceptionLine.java:38)
... 1 more
Caused by: com.vb.exception.HotelAgeException: 18岁以下\80岁以上的住客必须有亲友陪同
at com.vb.exception.ExceptionLine.testOne(ExceptionLine.java:21)
at com.vb.exception.ExceptionLine.testTwo(ExceptionLine.java:26)
... 2 more
*/
}
}
public static void testOne() throws HotelAgeException{
throw new HotelAgeException();
}
public static void testTwo() throws Exception{
try{
testOne();
}catch(HotelAgeException e){
// throw new Exception("testTwo 新产生的异常");
// 保留异常链之前的异常信息
// 方式一: 抛出的异常加上异常参数
throw new Exception("testTwo 新产生的异常", e);
}
}
public static void testThree()throws Exception{
try{
testTwo();
}catch(Exception e){
// throw new Exception("testThree 新产生的异常");
// throw new Exception("testThree 新产生的异常", e);
// 方式2: 在构造方法中描述字符串信息
Exception e1 = new Exception("testThree 新产生的异常");
e1.initCause(e);
throw e1;
}
}
}
1.8 异常总结
2. 包装类
- 什么是包装类?
- 包装类和基本数据类型之间的对应关系
- 包装类的常用方法
2.1 概述
- 由于基本数据类型没有属性方法, 无法和对象进行交互
- 包装类是拥有属性、方法,以及可以和对象进行交互
- 基本上数据类型和包装类的对应关系
2.2 基本数据类型和包装类之间的转换(装箱&拆箱)
装箱& 拆箱Code
public class WrapProj {
public static void main(String[] args) {
// 1.装箱: 把基本数据类型转换成包装类
// 1.1 自动装箱
int t1 = 6;
Integer t2 = t1;
// 1.2 手动装箱
Integer t3 = new Integer(t1);
// test
System.out.println("int类型变量t1= " + t1); // int类型变量t1= 6
System.out.println("Integer类型对象t2= " + t2); // Integer类型对象t2= 6
System.out.println("Integer类型对象t3= " + t3 ); // Integer类型对象t3= 6
// 2,拆箱 :把包装类转换成基本数据类型
// 2.1 自动拆箱
int t4 = t2;
// 2.2 手动拆箱
int t5 = t2.intValue();
// test
System.out.println("Integer 类型对象t2= "+t2); //Integer 类型对象t2= 6
System.out.println("自动拆箱后, int类型变量 t4=" +t4); //自动拆箱后, int类型变量 t4=6
System.out.println("手动拆箱后,int类型变量 t5 =" +t5); //手动拆箱后,int类型变量 t5 =6
}
2.3 基本数据类型和字符串之间的转换
-
String t2 = Integer.toString(t1);
-
int t3 = Integer.parseInt(t2);
-
int t4 = Integer.valueOf(t2);
代码演示:
public class WrapDemo02 {
public static void main(String[] args) {
// 01 .将基本上数据类型转换成字符串
int t1 = 2;
String t2 = Integer.toString(t1);
// test
System.out.println("int类型转换为String类型对象t2= " + t2); // int类型转换为String类型对象t2= 2
System.out.println("========================");
// 02. 字符串转换成基本数据类型
// 2.1 包装类的parse
int t3 = Integer.parseInt(t2);
// 2.2 包装类的valueOf 先将字符串转换为包装类, 再通过自动拆箱完成基本类型转换
int t4 = Integer.valueOf(t2);
// test
System.out.println("String类型转换为int类型变量t3= "+ t3); //String类型转换为int类型变量t3= 2
System.out.println("String类型转换为int类型变量t4= "+ t4); //String类型转换为int类型变量t4= 2
}
}
2.4 包装类&基本数据类型的初始值
包装类的数据初始值为null
代码演示:
public class Cat {
private String name;
private Integer month;
private Double weight;
public static void main(String[] args) {
Cat cat = new Cat();
System.out.println("String name: "+ cat.name); //String name: null
System.out.println("int month: "+ cat.month); //int month: 0
System.out.println("double weight: "+ cat.weight); //double weight: 0.0
System.out.println("Integer month: "+ cat.month); //Integer month: null
System.out.println("Double weight: "+ cat.weight); //Double weight: null
}
}
2.5 包装类对象之间的比较
3.String&StringBuilder&StringBuffer
- 如何创建String对象
- String对象的常用方法
- ==和equals的区别
- String的不可变性
- StringBuilder
3.1.创建String对象的方法
(4)利用字节数组创建
3.2.String对象的常用方法(!!!)
(1)length & subString & charAt
public class length_subString_charAt {
public static void main(String[] args) {
String str = new String("vb 学 Java 真爽 !!");
System.out.println(str.length()); //15
System.out.println(str.charAt(3)); //学
System.out.println(str.substring(3));//学 Java 真爽 !!
// 截止到 下标15 之前
System.out.println(str.substring(3, 15));//学 Java 真爽 !!
}
}
(2)indexOf & lastIndexOf
public class indexOf_lastIndexOf {
public static void main(String[] args) {
String str = new String();
str = "phdvb need more vb & give more vb";
// 查找'v' 在字符串种第一次出现的下表位置 (数组下标)
System.out.println(str.indexOf('v')); //3
// 查找"need" 在字符串第一次出现的位置
System.out.println(str.indexOf("need")); //6
// 查找'v' 在字符串种最后一次出现的下表位置
System.out.println(str.lastIndexOf('v')); //31
// 查找"vb" 在字符串种最后一次出现的下表位置
System.out.println(str.lastIndexOf("vb")); //31
// 从下标为10的位置开始,查找"vb" 在字符串种第一次出现的下表位置
System.out.println(str.indexOf("vb", 10)); //16
System.out.println(str.indexOf("vc")); //-1
System.out.println(str.indexOf('u')); //-1
}
}
(3)getBytes(字符串和byte数字间的相互转换 )&编码问题
GBK 和 UTF8 两种编码方式都可以用于编码汉字
代码实现:
public class getBytes_GBK_UTF8 {
public static void main(String[] args) throws UnsupportedEncodingException {
/**
* UTF8 的规则下 一个汉字需要使用3个字节来表示
*/
// 字符串 和 byte数组 之间的 相互转换
String str = "JAVA SE 基础";
// 将字符串 转换成 字符串
// byte[] arrs = str.getBytes();
byte[] arrs = str.getBytes("GBK");
for(int i = 0; i < str.length(); i++){
System.out.print(arrs[i] + " ");
/* (1) 编码格式为UTF8 的打印结果
74 65 86 65 32 83 69 32 -27 -97
*/
/* (2) GBK 的输出结果
74 65 86 65 32 83 69 32 -17 -65 -67 -17
*/
/* (3) str.getBytes("GBK");
*/
}
System.out.println();
// 将byte数组 转换为 字符串
// String str1 = new String(arrs);
String str1 = new String(arrs, "GBK");
System.out.println(str1); //JAVA SE 基础
/* byte[] arrs = str.getBytes("GBK"); String str1 = new String(arrs);
JAVA SE ����
*/
/* byte[] arrs = str.getBytes("GBK"); String str1 = new String(arrs, "GBK");
JAVA SE 基础
*/
}
}
3.3 equals() & ==
public class dengyu_equals {
public static void main(String[] args) {
// == 和 equals 的区别
String str1 = "phdvb";
String str2 = "phdvb";
String str3 = new String("phdvb");
String str4 = new String("phdvb");
// equals() --> 比较字符串间的值是否相同
System.out.println("str1 和 str2 的内容 相同?"+ (str1.equals(str2))); //true
System.out.println("str1 和 str3 的内容 相同?"+ (str1.equals(str3))); //true
System.out.println("str3 和 str4 的内容 相同?"+ (str4.equals(str3))); //true
// == --> 比较字符串间的地址是否相同
System.out.println("str1 和 str2 的地址 相同?"+ (str1 == str2)); //true
System.out.println("str1 和 str3 的地址 相同?"+ (str1 == str3)); //false
System.out.println("str3 和 str3 的地址 相同?"+ (str4 == str3)); //false
}
}
3.4 String的不可变性
3.5 StringBuilder
(1) String VS StringBuilder & StringBuffer
- String具有不可变性,而StringBuilder & StringBuffer具备可变性
- Tips: 当频繁操作字符串时, 使用StringBuilder or StringBuffer
(2) StringBuilder & StringBuffer
(3) StringBuilder 构造方法
(4)StringBuilder 成员方法
代码实现:
public class StringBuilderDemo {
public static void main(String[] args) {
// StringBuilder 定义一个 字符串
StringBuilder str = new StringBuilder("hi~");
// StringBuffer append(String str)
System.out.println(str.append("Phdvb,").append("你好呀~")); // hi~Phdvb,你好呀~
// 需求:将”hi~Phdvb,你好呀~“ 变为 "hi~PHDVB,你好呀~"
/**
* 方式1: 先delete ,在insert
* 方式2: 直接替换
*/
// System.out.println("方式1:"+ str.delete(4,8).insert(4,"HDVB")); //方式1:hi~PHDVB,你好呀~
System.out.println("方式2:"+str.replace(4,8,"HDVB")); //方式2:hi~PHDVB,你好呀~
// StringBuffer substring(int start, int end)
System.out.println(str.substring(3, 8)); //PHDVB
}
}