当前位置:网站首页>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
边栏推荐
- Go practice -- gorilla/rpc (gorilla/rpc/json) used by gorilla web Toolkit
- ES7 easy mistakes in index creation
- 1086 tree traversals again (25 points)
- Dynamic programming - related concepts, (tower problem)
- [set theory] relation properties (reflexivity | reflexivity theorem | reflexivity | reflexivity theorem | example)
- Pan details of deep learning
- 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
- Differences among bio, NiO and AIO
- [backtrader source code analysis 5] rewrite several time number conversion functions in utils with Python
- Redis 入门和数据类型讲解
猜你喜欢
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
JS dynamic table creation
Shallow and first code
[basic grammar] C language uses for loop to print Pentagram
Promise
Burp suite plug-in based on actual combat uses tips
联想R7000显卡的拆卸与安装
(subplots usage) Matplotlib how to draw multiple subgraphs (axis field)
Audio Focus Series: write a demo to understand audio focus and audiomananger
Three representations of signed numbers: original code, inverse code and complement code
随机推荐
Differences among bio, NiO and AIO
study hard and make progress every day
C language program ideas and several commonly used filters
Objects. Requirenonnull method description
50 practical applications of R language (36) - data visualization from basic to advanced
leetcode860. Lemonade change
The principle is simple, but I don't know how to use it? Understand "contemporaneous group model" in one article
Realize file download through the tag of < a > and customize the file name
1087 all roads lead to Rome (30 points)
[set theory] relational power operation (relational power operation | examples of relational power operation | properties of relational power operation)
Common interview questions of microservice
Go language interface learning notes Continued
Automatic voltage rise and fall 5-40v multi string super capacitor charging chip and solution
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
微服务常见面试题
112 stucked keyboard (20 points)
Unity tool Luban learning notes 1
小学校园IP网络广播-基于校园局域网的小学IP数字广播系统设计
JS scope
1110 complete binary tree (25 points)