Java进阶总结6
1. 代码块分类
/**
* 代码块 可划分为4类
* 1.局部代码块
* 2.初始化代码块
* 3.构造代码块
* 4.静态代码块
* */
package day11.block;
public class BlockDemo {
//局部代码块
public void method1() {
System.out.println("方法每调用一次会执行一次");
}
public static void method2() {
System.out.println("方法每调用一次会执行一次");
}
//初始化代码块 每创建一个对象的时候,会执行一次
{
System.out.println("初始化代码块优先于构造代码块");
}
//构造代码块 每创建一个对象的时候,会执行一次
public BlockDemo() {
System.out.println("构造代码块");
}
//静态代码块 当前类加载到JVM中时 会自动执行一次该代码块 且只会执行一次
static{
System.out.println("静态代码块");
}
public static void main(String[] args) {
//初始化一个BlockDemo对象
BlockDemo demo=new BlockDemo();
demo.method1();
BlockDemo demo1=new BlockDemo();
}
}
输出结果:
2. 包装类
/**
* 包装类:主要作用,将基本数据类型转换为引用数据类型
* 每个基本数据类型都对应一个包装类类型
* */
package day11.box;
public class BoxDemo {
public static void main(String[] args) {
//初始化一个数组 该数组只能存放引用数据类型 object类型
Object[]arr=new Object[3];
arr[1]="尊";
arr[0]=21;
//装箱 把基本数据类型转换成对应的包装类类型
//1.手动装箱方法有两种
//通过构造方法
Integer i=new Integer(22);//将一个整形转换成integer对象
//通过静态方法
Integer i1=Integer.valueOf(21);
//可以将string类型转换成Integer对象
Integer i2=new Integer("233");
//通过静态方法
Integer i3=Integer.valueOf("222");
//拆箱 把包装类类型转换成基本数据类型
//手动拆箱
int j=i.intValue();
System.out.println(j);
//将String开心转换成int类型
int j1=Integer.parseInt("123");
System.out.println(j1);
//自动进行装箱
Object[]obj={1,3,2,4,6};
Integer a=10;//编译器进行了自动装箱
System.out.println(a);
//自动拆箱
int b=a;//将引用数据类型赋值给基本数据类型,自动拆箱
System.out.println(b);
//进制转换
//十进制转二进制
System.out.println(Integer.toBinaryString(4));
//十进制转16进制
System.out.println(Integer.toHexString(41));
//十进制转8进制
System.out.println(Integer.toOctalString(10));
}
}
输出结果:
3. final修饰符
/**
* final 是个修饰符 可以修饰 类、变量、方法。
* final 如果和其他修饰符一起使用 可以没有先后顺序
* 例如:
* public final static
* final public static
* static final public
* */
/**
package day11.final1;
* final 修饰的类叫做最终类,不能被继承
* final 修饰符不能修饰抽象类
* 抽象类必须被继承才能使用
* */
// final修饰的类不能被继承
final class Demo{
}
//抽象类必须被继承才能使用
/*final abstract class Demo1{
}*/
/**
* 用final修饰的方法叫做最终方法,只能使用,不能重新
* 如果是静态方法,父类使用final修饰后,子类不能再定义重复的方法
* final不能修饰抽象方法,抽象方法必须被子类重写
* */
class Demo2{
//定义一个方法
public final void method1() {
System.out.println("学习");
}
public static void method2() {
}
// final不能修饰抽象方法,抽象方法必须被子类重写
/* public final static void method3() {
}*/
}
public class FinalDemo extends Demo2{
//如果父类的方法用final修饰了,子类不能重新final修饰的方法
/* @Override
public void method1() {
// TODO 自动生成的方法存根
super.method1();
//重新定义方法
System.out.println("好好学习");
}*/
//可以使用final修饰的方法
/* public static int method2() {
return 0;
}*/
/**
* final 修饰的字段叫做最终量,只能被赋值一次,不能再次改变
* final修饰的字段必须附上初始值
* */
//定义一个最终量
//final修饰基本数据类型
final static int age=0;
//final修饰引用数据类型
final static int[]num=new int[2];
//final修饰的变量叫做常量 不可改变量
//声明一个常量 用public final static修饰,变量名全部大写,多个单词之间用——连接
public final static int MAX_VALUE=123123;
public static void main(String[] args) {
System.out.println(age);
//final修饰的变量不能再次改变
//age=1;
//更改num的是引用还是内容?
//num=new int[3];//引用数据类型不能更改引用的内存
num[0]=12;
num[1]=11;//引用数据类型的内容可以更改,但引用不可以被更改
}
}
4. 异常处理机制
4.1 捕获异常
/**
* 捕获异常
* try{
* //有可能出现异常的代码
* }catch(Exception e){//catch语句可以有多个
* //捕获异常
* }finallt{
* //无论有无异常,都要执行
* }
* try不能单独使用
* catch捕获多个异常的时候
* 先子类异常,再父类异常
* */
package day11.exception;
public class Exception1 {
public static void main(String[] args) {
// 声明两个变量
int num=1;
int num1=0;
int[]arr=new int[1];
//try--catch结合使用
try {//存放有可能出现异常的代码
//如果出现异常,没有对异常进行处理,以下代码将不会执行
System.out.println(num/num1);
} catch (Exception e) {//捕获异常,对异常进行处理,保证程序继续运行
// 处理异常
System.out.println(e.getMessage());
//打印输出所有的异常印象
e.printStackTrace();
}
//try--finally结合使用
try {
arr[0]=1;
//arr[1]=2;
} finally {
// 无论有无异常,该代码块都会执行,如果没有catch语句,执行完该代码块,程序中断
System.out.println("一定执行");
}
//try--catch--finally结合使用
try {
System.out.println(num/num1);//算术异常
System.out.println(arr[1]);//数组下标越界
} catch (ArithmeticException e) {
// TODO: handle exception
e.printStackTrace();
}catch (ArrayIndexOutOfBoundsException e) {
// TODO: handle exception
e.printStackTrace();
}catch (Exception e) {
// TODO: handle exception
}finally {
System.out.println("最终执行的代码块");
}
System.out.println("Hello Exception!");
}
}
输出结果:
4.2 终止finally语句
/**
* try--catch--finally语句
* 如果使用return语句,必须先执行完finally语句,再执行return语句
* Sysout.exit(0);语句可以终止finally语句
* */
package day11.exception;
public class Exception2 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
try {
int a=10;
int b=0;
System.out.println("a/b="+a/b);
//return;
} catch (Exception e) {
// TODO: handle exception
System.out.println("捕获异常");
System.exit(0);//终止虚拟机
//return;
}finally {
//return;//不可达代码
System.out.println("最终执行");
}
System.out.println("所有操作执行完毕");
}
}
输出结果:
4.3 声明异常
/**
* 声明异常 使用关键字 throws
* 语法:修饰符 返回值类型 方法名(参数列表) throws 异常类型1,异常类型2...{
*
* }
* 作用:告诉方法调用者,调用该方法可能会产生的异常类型,调用者需要对异常进行处理
* 异常传递链过程中
* 不会创建新的异常信息,会保留原来的异常信息,继续向上传递
* */
package day11.exception;
public class Exception3 {
//定义方法
public static void method1() throws Exception{
//求两个数的商
int a=10;
int b=10;
System.out.println("a/b="+a/b);//可能产生异常,不对该异常进行处理
//调用method2()
method2();
}
public static void method2() throws ArrayIndexOutOfBoundsException{
//操作数组
int[]arr={1};
System.out.println(arr[2]);//不做处理,只说明,谁调用谁处理
}
public static void main(String[] args) {
//调用者捕获异常信息
try {
method1();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
/* try {
method2();
} catch (ArrayIndexOutOfBoundsException e) {
// TODO: handle exception
e.printStackTrace();
}*/
System.out.println("异常处理完毕");
}
}
输出结果:
4.4 Throw 关键字 抛出异常
/**
* Throw 关键字 抛出异常
* 一旦手动抛出一个异常,throw以下代码将不会执行,类似return
* throw 异常对象
* 在使用throw的时候,必须声明异常
* */
package day11.exception;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class Exception4 {
public static void method1() throws Exception {
System.out.println("Hello");
//创建一个异常对象
Exception e=new Exception("异常的信息");
//手动抛出异常
throw e;
//System.out.println("Nice to meet you!");
}
//编译异常,编译的时候必须进行异常声明
public static void method2() throws FileNotFoundException {
//文件操作
File file=new File("");
//打开文件
FileInputStream in=new FileInputStream(file);
}
//在运行时可能出现异常信息,运行时异常,也是需要对异常进行处理
public void method3() {
int a=10;
int b=0;
System.out.println("a/b="+a/b);
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
try {
method1();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
输出结果:
5.知识框架