当前位置:网站首页>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
边栏推荐
- "250000 a year is just the price of cabbage" has become a thing of the past. The annual salary of AI posts has decreased by 8.9%, and the latest salary report has been released
- BIO、NIO、AIO区别
- (完美解决)matplotlib图例(legend)如何自由设置其位置
- Messy change of mouse style in win system
- Interview question -- output the same characters in two character arrays
- appium1.22. Appium inspector after X version needs to be installed separately
- XML配置文件
- 音频焦点系列:手写一个demo理解音频焦点与AudioMananger
- Go practice -- gorilla / websocket used by gorilla web Toolkit
- 编译GCC遇到的“pthread.h” not found问题
猜你喜欢
Pan details of deep learning
[research materials] annual report of China's pension market in 2021 - Download attached
[research materials] 2021 annual report on mergers and acquisitions in the property management industry - Download attached
Webrtc protocol introduction -- an article to understand ice, stun, NAT, turn
Technical analysis of qianyuantong multi card aggregation router
【批处理DOS-CMD命令-汇总和小结】-CMD窗口的设置与操作命令-关闭cmd窗口、退出cmd环境(exit、exit /b、goto :eof)
Disassembly and installation of Lenovo r7000 graphics card
"Hands on deep learning" pytorch edition Chapter II exercise
[basic grammar] Snake game written in C language
联想R7000显卡的拆卸与安装
随机推荐
Notes | numpy-09 Broadcast
酒店公共广播背景音乐-基于互联网+的酒店IP网络广播系统设计
My first Smartphone
Introduction to rust Foundation (basic type)
The consumption of Internet of things users is only 76 cents, and the price has become the biggest obstacle to the promotion of 5g industrial interconnection
Audio Focus Series: write a demo to understand audio focus and audiomananger
1110 complete binary tree (25 points)
Learn libcef together -- set cookies for your browser
Introduction to deep learning (II) -- univariate linear regression
Go practice -- gorilla/rpc (gorilla/rpc/json) used by gorilla web Toolkit
Notes | numpy-07 Slice and index
Intégration profonde et alignement des séquences de protéines Google
1118 birds in forest (25 points)
(subplots usage) Matplotlib how to draw multiple subgraphs (axis field)
Differences among bio, NiO and AIO
Online VR model display - 3D visual display solution
[research materials] 2022q1 game preferred casual game distribution circular - Download attached
Pessimistic lock and optimistic lock of multithreading
1099 build a binary search tree (30 points)
Yolov5 input (II) | CSDN creative punch in