当前位置:网站首页>fianl、finally、finalize三者的区别
fianl、finally、finalize三者的区别
2022-07-06 09:20:00 【快醒醒鸭今天你编程了吗?】
首先这三者其实是没有本质关系的,面试的时候会考察我们对这块的理解:
fianl
fianl是用来修饰变量,修饰类,修饰类属性,当它修饰变量的时候,变量的引用不可变但是地址中的内容可以改变,当它修饰类的时候,这个类不可被继承但是可以被重载,当它修饰类属性的时候,子类就不能给这个属性重新赋值,如果赋值会报错。
finally
常用语try catch finally中,在异常处理时用finally执行任何清除操作,不管有没有异常抛出和捕获finally中的语句都会执行
如果try代码块里面有return语句,return语句还一一定要是最后执行的,java中有一条亘古不变的规则,程序是自上而下逐行执行,一旦执行了return整个方法结束。
咱们看一道题目:
package com.ws;
public class Demo02 {
public static void main(String[] args) {
int result = m();
System.out.println(result);
}
public static int m(){
int i = 100;
try{
return i;
}finally {
i++;
}
}
}
以上输出结果是什么?我猜大多数人肯定会说是101,但是答案是100
咱们来看看反编译之后的代码
//以下就是反编译之后的代码
public static int m(){
int i = 100;
int j = i;
i++;
return j;
Exception exception;
exception;
i++;
throw exception;
}
首先是把i的值给j,然后i++(就是执行fianlly里面的代码),然后返回j。
finalize
在垃圾回收中,如果被回收的对象重写了finalize()方法,并且在finalize()方法中重新建立了该对象与GC ROOT的连接,那么该对象是可以被复活一次的,也就是该对象会从收集阶段重新回到应用阶段。
以上就是这三者的区别!!!
边栏推荐
- 5. Function recursion exercise
- vector
- 3.C语言用代数余子式计算行列式
- MySQL Database Constraints
- Implement queue with stack
- Alibaba cloud microservices (I) service registry Nacos, rest template and feign client
- Alibaba cloud microservices (IV) service mesh overview and instance istio
- JS interview questions (I)
- TYUT太原理工大学2022数据库考试题型大纲
- Arduino+ water level sensor +led display + buzzer alarm
猜你喜欢
Smart classroom solution and mobile teaching concept description
System design learning (III) design Amazon's sales rank by category feature
Alibaba cloud microservices (IV) service mesh overview and instance istio
西安电子科技大学22学年上学期《信号与系统》试题及答案
Alibaba cloud microservices (II) distributed service configuration center and Nacos usage scenarios and implementation introduction
最新坦克大战2022-全程开发笔记-2
Alibaba cloud microservices (III) sentinel open source flow control fuse degradation component
Counter attack of flour dregs: redis series 52 questions, 30000 words + 80 pictures in detail.
IPv6 experiment
2.初识C语言(2)
随机推荐
Rich Shenzhen people and renting Shenzhen people
CorelDRAW plug-in -- GMS plug-in development -- Introduction to VBA -- GMS plug-in installation -- Security -- macro Manager -- CDR plug-in (I)
MySQL 30000 word essence summary + 100 interview questions, hanging the interviewer is more than enough (Collection Series
3. Number guessing game
【快趁你舍友打游戏,来看道题吧】
4.30动态内存分配笔记
2.初识C语言(2)
vector
【话题终结者】
TYUT太原理工大学2022数据库之关系代数小题
arduino+DS18B20温度传感器(蜂鸣器报警)+LCD1602显示(IIC驱动)
TYUT太原理工大学2022“mao gai”必背
C语言入门指南
5. Function recursion exercise
A brief introduction to the database of tyut Taiyuan University of technology in previous years
Counter attack of flour dregs: redis series 52 questions, 30000 words + 80 pictures in detail.
string
凡人修仙学指针-1
Branch and loop statements
20220211-CTF-MISC-006-pure_ Color (use of stegsolve tool) -007 Aesop_ Secret (AES decryption)