当前位置:网站首页>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
边栏推荐
- 1099 build a binary search tree (30 points)
- 1086 tree traversals again (25 points)
- "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
- Without 50W bride price, my girlfriend was forcibly dragged away. What should I do
- Huawei personally ended up developing 5g RF chips, breaking the monopoly of Japan and the United States
- [research materials] 2021 annual report on mergers and acquisitions in the property management industry - Download attached
- leetcode435. Non overlapping interval
- 微服务常见面试题
- Differences among bio, NiO and AIO
- [set theory] relational power operation (relational power operation | examples of relational power operation | properties of relational power operation)
猜你喜欢

XML Configuration File
![[set theory] relationship properties (symmetry | symmetry examples | symmetry related theorems | antisymmetry | antisymmetry examples | antisymmetry theorems)](/img/34/d195752992f8955bc2a41b4ce751db.jpg)
[set theory] relationship properties (symmetry | symmetry examples | symmetry related theorems | antisymmetry | antisymmetry examples | antisymmetry theorems)

Go practice -- design patterns in golang's singleton

Appium 1.22. L'Inspecteur appium après la version X doit être installé séparément

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

Yolov5 network structure + code + application details | CSDN creation punch in
![[set theory] relation properties (reflexivity | reflexivity theorem | reflexivity | reflexivity theorem | example)](/img/2a/362f3b0491f721d89336d4f468c9dd.jpg)
[set theory] relation properties (reflexivity | reflexivity theorem | reflexivity | reflexivity theorem | example)

Congratulations to musk and NADELLA on their election as academicians of the American Academy of engineering, and Zhang Hongjiang and Fang daining on their election as foreign academicians

Shuttle + alluxio accelerated memory shuffle take-off

appium1.22.x 版本後的 appium inspector 需單獨安裝
随机推荐
Notes | numpy-09 Broadcast
[research materials] 2021 annual report on mergers and acquisitions in the property management industry - Download attached
Introduction to rust Foundation (basic type)
leetcode435. Non overlapping interval
Go practice -- gorilla / websocket used by gorilla web Toolkit
Make your own dataset
穀歌 | 蛋白序列的深度嵌入和比對
[develop wechat applet local storage with uni app]
cookie session jwt
Realize file download through the tag of < a > and customize the file name
Kept hot standby and haproxy
1099 build a binary search tree (30 points)
编译GCC遇到的“pthread.h” not found问题
微服务常见面试题
Use posture of sudo right raising vulnerability in actual combat (cve-2021-3156)
Why is go language particularly popular in China
My first Smartphone
Notes | numpy-07 Slice and index
[set theory] relationship properties (common relationship properties | relationship properties examples | relationship operation properties)
JS scope