通过查看Structure的结构我们可知:StringBuilder 的的方法基本上可以归为5种,分别是:
append
,delete
,replace
,insert
,indexOf
其中最关键的是“append”,使用案例如下所示:
public class Main {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("123");
stringBuffer.append("abc").append("excellent");
System.out.println(stringBuffer);
StringBuilder stringBuilder = new StringBuilder("Hi");
stringBuilder.append(" Hello");
stringBuilder.append(" Aileen ").append("How are you?");
System.out.println(stringBuilder);
}
}
通过查看StringBuffer和StringBuilder这两个方法的append方法的重写,我们可以得出:这两个方法最主要的区别如下:
StringBuffer方法适用于多线程情况
。(eg:多人排队上一个卫生间,只有当卫生间里面没人了下一个人才能进去上厕所。)
public class Main {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("123");
stringBuffer.append("abc").append("excellent");
System.out.println(stringBuffer);
StringBuilder stringBuilder = new StringBuilder("Hi");
stringBuilder.append(" Hello");
stringBuilder.append(" Aileen ").append("How are you?");
System.out.println(stringBuilder);
System.out.println("=================================");
// 将stringBuilder转化成:string
String str = stringBuilder.toString();
System.out.println(str);
System.out.println("----------------------------------");
// 将string转换成stringBuilder - 调用构造方法
String str1 = "aileen";
StringBuilder stringBuilder1 = new StringBuilder(str1);
System.out.println(stringBuilder1);
System.out.println("----------------------------------");
//
StringBuilder stringBuilder2 = new StringBuilder();
stringBuilder2.append(str1);
System.out.println(stringBuilder2);
}
}
面试题 |
---|
public static void main(String[] args) {
int[] array = {1,2,3,4};
System.out.println(array[10]);
System.out.println("after");
}
public static void main(String[] args) {
int[] array = null;
System.out.println(array[10]);
System.out.println("Hi");
}
public static void func(){
func();
}
public static void main(String[] args) {
func();
}
public static void func2(int[] array){
if(array == null){
throw new NullPointerException("array == null 了!");
}
}
public static void main(String[] args) {
func2(null);
}
异常一旦抛出,后面的代码就不会执行了。
class Person {
public String name;
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class ExceptionLearn {
public static void main(String[] args) throws CloneNotSupportedException {
Person p = new Person();
Person p2 = (Person) p.clone();
System.out.println("hi");
}
}
因为throws抛出的异常有两种情况,一种是运行时异常;另一种是编译时异常。 而对于main方法,他会把func()这里调用抛出的异常当做一个编译时异常来处理,这就需要程序员手动处理。以下是处理的方式及过程:
1.通过throws对异常进行层层声明,,然后程序会将异常交给JVM处理,此时程序就会异常终止。
2.通过try-catch抛出异常,用时程序还能够继续向下执行。如果不在main方法中throws Exception,可以通过快捷键Alt+Enter
选中以下的内容。
public class Test
{
public static void func() throws NullPointerException{
int[] array = null;
System.out.println(array.length);
}
public static void main(String[] args){
try {
func();//将可能出现异常的代码放这
} catch (NullPointerException e) {//捕获对应的异常
System.out.println("处理异常");
}
System.out.println("after");
}
}
如果未在catch处声明对应代码的异常,再次运行程序结果会怎么样呢?
public static void main(String[] args) {
try{
int[] array = {1,2,3,4,5};
System.out.println(array[9]);
}catch (NullPointerException e){
e.printStackTrace();
System.out.println("捕获到了空指针异常。。。");
}
System.out.println("after");
}
通过上面的代码我们可以看出,如果我们没有将对应代码的异常通过catch去捕获,那么它就会通过我们的最后一道防线JVM去终止程序。
我们看到对于异常代码我们运行完以后直接通过catch去捕获其对应的异常,而对于异常代码后面的sout语句它并未执行。
public static void main(String[] args) {
try {
int[] array = {1, 2, 3, 4, 5};
System.out.println(array[9]);
}catch (NullPointerException | ArrayIndexOutOfBoundsException e){ //可以通过|分割捕获多个异常,但是缺点是无法知道具体是哪一个
e.printStackTrace();
System.out.println("捕获到空指针 | 数组越界异常......");
}
System.out.println("Aileen");
}
public static void main(String[] args) {
int[] arr = {1,2,3};
try{
System.out.println("before");
arr = null;
System.out.println(arr[100]);
System.out.println("after");
}catch(NullPointerException e){//空指针异常,属于异常的子类
e.printStackTrace();
System.out.println("我是空指针异常~");
}
catch(Exception e){//可捕获到所有异常,所有异常的父类
e.printStackTrace();
}
System.out.println("after try catch");
}
想要用try catch
只是捕获简单的异常直接catchException
即可,如果想要知道具体的异常类型可使用多个catch去捕获这个异常的具体内容
。
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try{
int[] array = {1,2,3,4};
System.out.println(array[10]);
int a = scanner.nextInt();
System.out.println(a);
}catch (NullPointerException e){
e.printStackTrace();
System.out.println("捕获NullPointerException异常...");
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("捕获ArrayIndexOutOfBoundsException异常...");
}finally {
System.out.println("finally被执行了");
scanner.close();
}
System.out.println("after");
}
import java.util.Scanner;
public class Test {
public static int func2() {
Scanner scanner = new Scanner(System.in);
try {
int a = scanner.nextInt();
System.out.println(a);
} catch (NullPointerException e) {
e.printStackTrace();
System.out.println("捕获NullPointerException异常...");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("捕获ArrayIndexOutOfBoundsException异常...");
} finally {
System.out.println("finally被执行了");
scanner.close();
}
System.out.println("after");
return -1;
}
public static void main(String[] args) {
func2();
}
根据上面的运行结果可以得出以下结论:当我们try-catch
捕获异常时,如果catch后面的异常跟我们出现的不匹配,它就会通过JVM
来找到异常,并且终止程序。上面的scanner需要我们输入数字但是我输入的是字符与需求不匹配,所以当程序运行到这里的时候会通过JVM
去捕获异常,然后终止程序,finally永远都会被执行,正因如此如果我们需要关闭资源我们可以将关闭资源的代码放入到finally的语句块中,从而节省程序的资源
。
import java.util.InputMismatchException;
import java.util.Scanner;
public class Test {
public static int func2() {
Scanner scanner = new Scanner(System.in);
try {
int a = 10;
return a;
} catch (InputMismatchException e) {
e.printStackTrace();
System.out.println("捕获InputMismatchException异常...");
} finally {
System.out.println("finally被执行了");
scanner.close();
return 99;
}
}
public static void main(String[] args) {
System.out.println(func2());
}
try
中的代码try
中的代码出现异常,就会结束try
中的代码,看和catch中的异常类型是否匹配。catch
中的代码finally
中的代码都会被执行到(在该方法结束前执行)。JVM
来处理,此时程序就会异常终止。import java.util.InputMismatchException;
import java.util.Scanner;
class Login{
public String userName = "Aileen";
public String passWord = "123456";
public void logIn(String userName , String passWord){
if(!this.userName.equals(userName)){
System.out.println("用户名错误!");
}
if(!this.passWord.equals(passWord)){
System.out.println("密码错误!");
}
}
}
public class Test {
public static void main(String[] args) {
Login login = new Login();
login.logIn("Aileen","abcd");
}
我们可以通过自定义方法来找到错误点但是无法定位到错误的位置,为了实现这个功能,我们可以自定义一个异常类,来定位错误,我们可以通过模仿已有的异常来编写自定义异常的类:
我们可以看到已有异常继承于运行时异常这个类,并且还调用了其无参构造方法和带有一个参数的构造方法。
throw
来抛出对应的自定义异常import java.util.InputMismatchException;
import java.util.Scanner;
class Login{
public String userName = "Aileen";
public String passWord = "123456";
public void logIn(String userName , String passWord){
if(!this.userName.equals(userName)){
// System.out.println("用户名错误!");
throw new UserNameException("用户名错误!");
}
if(!this.passWord.equals(passWord)){
// System.out.println("密码错误!");
throw new PassWordException("密码错误!");
}
}
}
public class Test {
public static void main(String[] args) {
try{
Login login = new Login();
login.logIn("Aileen","abcd");
}catch (UserNameException e){
e.printStackTrace();
System.out.println("捕捉到了用户名错误异常。。。");
}catch (PassWordException e){
e.printStackTrace();
System.out.println("捕捉到了密码错误异常。。。");
}
System.out.println("程序继续执行");
}