当前位置:网站首页>final、finally、finalize的区别
final、finally、finalize的区别
2022-07-22 05:39:00 【咸鱼吐泡泡】
下面重点讲final
一:final
final的定义:final是一个关键字,final可以修饰类、属性、方法、局部变量。
使用final的情形
- 不希望类被继承时,可以使用final修饰
- 不希望父类的某个方法被子类覆盖/重写时,可以使用final修饰
- 当不希望类中的某个属性的值被修改,可以使用final修饰
- 当不希望某个局部变量被修改,可以使用final修饰
final使用的注意细节
- final修饰的属性叫做常量,一般用XX_XX_XX来命名
- final修饰的属性在定义时必须被赋初值,并且以后不能再被修改
- final被赋值的位置可以是定义时、构造器中、代码块中
- final修饰的属性如果是静态的,不能在构造器中赋值
- 包装类、String都是final类,不能被继承
二:finally
finally的定义:
finally是一个关键字,用在异常处理机制中。表示不管有没有发生异常都要执行finally代码里面的逻辑,所以finally里面主要写关闭资源的操作。
使用finally的注意细节
- finally里面尽量不要写return语句,因为写了以后会覆盖掉try-catch中的return
演示细节:
public class Exception {
public static int method(){
try {
String[] names = new String[3];
if(names[1].equals("zhangsan")){
System.out.println(names[1]);
}else{
names[3] = "hspedu";
}
return 1;
} catch (ArrayIndexOutOfBoundsException e) {
return 2;
} catch(NullPointerException e){
return 3;
} finally{
return 4;
}
}
public static void main(String[] args) {
System.out.println(method());
}
}
上面代码输出的结果是4,因为finally是最后执行,然后它里面有return,这是相当于return 3就没用了,因为被覆盖掉了。
public class Exception {
public static int method(){
try {
String[] names = new String[3];
if(names[1].equals("zhangsan")){
System.out.println(names[1]);
}else{
names[3] = "hspedu";
}
return 1;
} catch (ArrayIndexOutOfBoundsException e) {
return 2;
} catch(NullPointerException e){
return 3;
} finally{
System.out.println(4);
}
}
public static void main(String[] args) {
System.out.println(method());
}
}
现在输出的结果就是4、3。虽然先执行的是return 3,但是他后面还有finally,有就必须执行。执行完以后才能返回。
三:finalize
在从堆中永久删除对象之前,垃圾回收器调用该对象的finalize()方法,做一些释放资源的操作。
边栏推荐
- Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
- Some new features in ES6
- tf.reduce_sum()
- Application of tensorflow optimizers in iris classification task
- 汉得企业级数字化PaaS平台 HZERO 1.9.0 版本正式发布!
- Getting started with VIM
- Blob URL DataURL
- Vivo official website app full model UI adaptation scheme
- Contract awarding and use of NPM private server
- UE4 lifts the elevator by pressing the key
猜你喜欢
MySQL foundation +mysql cluster review
tf.reduce_sum()
使用OpenCV实现哈哈镜效果
工作流引擎在vivo营销自动化中的应用实践 | 引擎篇03
Matlab function: filtfilter -- zero phase digital filtering
JSON_ Incorrect problem returned by extract
keepalived
npm私服发包及使用
Hande enterprise PAAS platform hzero will soon be heavily open source!
UE4 set night (update skysphere according to directionallight direction)
随机推荐
14-多线程1
numpy.random.seed()
Matlab function: filtfilter -- zero phase digital filtering
apache虚拟主机配置的方式有哪些
Hande enterprise digital PAAS platform hzero version 1.9.0 was officially released!
keepalived
UE4 merge static mesh body
UE4 keyboard keys realize door opening and closing
ig,ax = plt.subplots()
Blob URL DataURL
UE4 create a project
活动推荐| Apache Pulsar 在 vivo 的探索与实践 即将开播
Hande x Jiuli special materials | work together to create a collaborative office portal and help it internal standardized management
进程与线程的区别
vivo官网APP全机型UI适配方案
pytorch优化器: optim.SGD && optimizer.zero_grad()
PyGame electronic warfare simulation effect
Hiam fine-grained unified authorization management helps enterprises achieve refined management and control of system permissions
1840. The highest building height is greedy
tf.compat