当前位置:网站首页>Explanation of several points needing attention in final (tested by the author)
Explanation of several points needing attention in final (tested by the author)
2022-07-03 05:16:00 【A wild man about to succeed】
First we test final Impact on non static variables and methods
Yes final Under the circumstances :
public class FinalTest {
final int a = 1;
final String s = "sss";
final void haha() {
System.out.println("haha");
System.out.println(a);
}
public static void main(String[] args) {
FinalTest finalTest = new FinalTest();
}
}Decompile bytecode file :
public class com.heaboy.mvc.FinalTest {
final int a;
final java.lang.String s;
public com.heaboy.mvc.FinalTest();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: aload_0
5: iconst_1
6: putfield #2 // Field a:I
9: aload_0
10: ldc #3 // String sss
12: putfield #4 // Field s:Ljava/lang/String;
15: return
final void haha();
Code:
0: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #6 // String haha
5: invokevirtual #7 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream;
11: iconst_1
12: invokevirtual #9 // Method java/io/PrintStream.println:(I)V
15: return
public static void main(java.lang.String[]);
Code:
0: new #8 // class com/heaboy/mvc/FinalTest
3: dup
4: invokespecial #10 // Method "<init>":()V
7: astore_1
8: return
}nothing final Under the circumstances :
public class FinalTest {
int a = 1;
String s = "sss";
void haha() {
System.out.println("haha");
System.out.println(a);
}
public static void main(String[] args) {
FinalTest finalTest = new FinalTest();
}
}Decompile bytecode file :
public class com.heaboy.mvc.FinalTest {
int a;
java.lang.String s;
public com.heaboy.mvc.FinalTest();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: aload_0
5: iconst_1
6: putfield #2 // Field a:I
9: aload_0
10: ldc #3 // String sss
12: putfield #4 // Field s:Ljava/lang/String;
15: return
void haha();
Code:
0: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #6 // String haha
5: invokevirtual #7 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream;
11: aload_0
12: getfield #2 // Field a:I
15: invokevirtual #8 // Method java/io/PrintStream.println:(I)V
18: return
public static void main(java.lang.String[]);
Code:
0: new #9 // class com/heaboy/mvc/FinalTest
3: dup
4: invokespecial #10 // Method "<init>":()V
7: astore_1
8: return
}All in all ,final In both haha() There is a little difference in the method :
What we output here is a:
No, final Decorated variable a, We need to aload_0,getfield, That is, load variables , Get the value of the variable , Then the output
- Yes final Decorated variable a, We just need to load iconst_1 perhaps sipush( This is a big number , For example, we give a The initial assignment is 10, This is big number ,jvm You don't use it iconst_1, Instead, choose to use sipush To perform the operation of assignment ), That is, directly load constants , Direct output
- Theoretically final The modified constant call is faster
Then we test final Yes Static variables and methods Influence
Yes final Under the circumstances :
public class FinalTest {
final static int a = 1;
final static String s = "sss";
final static void haha() {
System.out.println(a);
}
public static void main(String[] args) {
FinalTest finalTest = new FinalTest();
}
}Decompile bytecode file :
public class com.heaboy.mvc.FinalTest {
static final int a;
static final java.lang.String s;
public com.heaboy.mvc.FinalTest();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
static final void haha();
Code:
0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
3: iconst_1
4: invokevirtual #4 // Method java/io/PrintStream.println:(I)V
7: return
public static void main(java.lang.String[]);
Code:
0: new #3 // class com/heaboy/mvc/FinalTest
3: dup
4: invokespecial #5 // Method "<init>":()V
7: astore_1
8: return
}No, final Under the circumstances :
public class FinalTest {
static int a = 1;
static String s = "sss";
static void haha() {
System.out.println(a);
}
public static void main(String[] args) {
FinalTest finalTest = new FinalTest();
}
}Decompile bytecode file :
public class com.heaboy.mvc.FinalTest {
static int a;
static java.lang.String s;
public com.heaboy.mvc.FinalTest();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
static void haha();
Code:
0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
3: getstatic #3 // Field a:I
6: invokevirtual #4 // Method java/io/PrintStream.println:(I)V
9: return
public static void main(java.lang.String[]);
Code:
0: new #5 // class com/heaboy/mvc/FinalTest
3: dup
4: invokespecial #6 // Method "<init>":()V
7: astore_1
8: return
static {};
Code:
0: iconst_1
1: putstatic #3 // Field a:I
4: ldc #7 // String sss
6: putstatic #8 // Field s:Ljava/lang/String;
9: return
}All in all ,final The variables decorated are different , The two haha() There is also a little difference in the method :
- final The decorated variable is not in the static area , But in the constant pool
What we output here is a:
No, final Decorated variable a, We need to getstatic, That is, load static variables , Then the output
- Yes final Decorated variable a, We just need to load iconst_1 perhaps sipush That is, directly load constants , Direct output
Father son inheritance
- final Modified classes cannot be inherited
- final Decorated objects and arrays , We can modify the contents , But you can't change the address
- We cannot re assign values to variables of the parent class , And cannot override the method of the parent class
We test the first case : Will report a mistake , Cannot inherit final Modified class
public class FinalTest extends FinalDadTest {
}
final class FinalDadTest {
}
When we test the second case, we will output 1111: We can figure out ,final Decorated objects and arrays , We can modify the contents , But you can't change the address
public class FinalTest extends FinalDadTest {
public static void main(String[] args) {
final FinalTest finalTest = new FinalTest();
final int[] arr = finalTest.arr;
arr[0] = 1111;
System.out.println(arr[0]);// Output 1111
}
}
class FinalDadTest {
final int a = 1;
final int[] arr = new int[10];
final void haha() {
System.out.println("sss");
}
final void cece() {
System.out.println("cece");
}
}
We test the third case : We will find that we cannot give the parent class a Reassign , And cannot override the method of the parent class haha().
public class FinalTest extends FinalDadTest {
public FinalTest() {
super.a = 2;
}
void haha() {
System.out.println(a);
}
public static void main(String[] args) {
}
}
class FinalDadTest {
final int a = 1;
final void haha() {
System.out.println("sss");
}
}end
边栏推荐
- Notes | numpy-08 Advanced index
- The IntelliJ platform completely disables the log4j component
- 112 stucked keyboard (20 points)
- Configure and use Anaconda environment in pycharm
- Use posture of sudo right raising vulnerability in actual combat (cve-2021-3156)
- The process of browser accessing the website
- Burp suite plug-in based on actual combat uses tips
- 1094 the largest generation (25 points)
- 音频焦点系列:手写一个demo理解音频焦点与AudioMananger
- Covering Safari and edge, almost all mainstream browsers have realized webgl 2.0 support
猜你喜欢
![[set theory] relationship properties (common relationship properties | relationship properties examples | relationship operation properties)](/img/af/8dfa783c87363a9d75c52e7680d508.jpg)
[set theory] relationship properties (common relationship properties | relationship properties examples | relationship operation properties)

Three representations of signed numbers: original code, inverse code and complement code

Celebrate the new year together

Gbase8s unique index and non unique index

BIO、NIO、AIO区别
![[Yu Yue education] basic reference materials of interchangeability and measurement technology of Zhongyuan Institute of Technology](/img/f1/d0dc4dc3fe49a2d2cd9e452a0ce31e.jpg)
[Yu Yue education] basic reference materials of interchangeability and measurement technology of Zhongyuan Institute of Technology

Deep embedding and alignment of Google | protein sequences

酒店公共广播背景音乐-基于互联网+的酒店IP网络广播系统设计

Go practice - gorilla / handlers used by gorilla web Toolkit

BTC-密码学原理
随机推荐
Three representations of signed numbers: original code, inverse code and complement code
112 stucked keyboard (20 points)
Go practice -- use JWT (JSON web token) in golang
The IntelliJ platform completely disables the log4j component
[research materials] 2021 China's game industry brand report - Download attached
[set theory] relationship properties (common relationship properties | relationship properties examples | relationship operation properties)
JS dynamic table creation
Use posture of sudo right raising vulnerability in actual combat (cve-2021-3156)
大学校园IP网络广播-厂家基于校园局域网的大学校园IP广播方案设计指南
Source insight garbled code solution
RT thread flow notes I startup, schedule, thread
Basic knowledge of reflection (detailed explanation)
[research materials] the fourth quarter report of the survey of Chinese small and micro entrepreneurs in 2021 - Download attached
Realize file download through the tag of < a > and customize the file name
Interview question -- output the same characters in two character arrays
Self introduction and objectives
Yolov5 model construction source code details | CSDN creation punch in
Webrtc native M96 version opening trip -- a reading code download and compilation (Ninja GN depot_tools)
My first Smartphone
1099 build a binary search tree (30 points)