In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to use the final keyword in Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
one。 Final class
A class modified by final, which cannot be inherited. When you confirm that a class will never be inherited or do not want to be inherited, you can decorate it with final.
Similarly, interfaces (interface) and abstract classes (abstract Class) are designed for "polymorphism" and cannot be modified with the final keyword.
Member methods in the final class are also implicitly specified as final methods by default.
two。 Final method
Final decorated methods cannot be overridden.
Example:
/ * * parent class * @ author LH * / public class FinalDemo1 {public final void test () {}}
three。 Final variable
Final variables include member variables and local variables. Variable types include basic data types and objects.
Using final to modify local primitive type variables (and their wrapper classes), values are immutable once initialized (when they can be defined or before they are used). Such as:
Final int a = 0There a = 1Tracer / error final int BTX b = 1Tracer / compile passed
When a local reference type variable is modified by final, the object (memory address) referenced by it (which can be initialized when defined or before use) is immutable, but the data stored in the object can be changed.
Public static void main (String [] args) {final String str1 = "helloWorld"; str1 = "helloChina"; / / compilation error, immutability of String, the new object reference is returned here. Final StringBuilder sb = new StringBuilder ("hello"); sb.append ("world"); / / compiled by sb = new StringBuilder ("China"); / / compilation error}
Final-modified member variables must be initialized directly at the time of definition, otherwise compilation errors will occur.
Public class FinalDemo1 {basic type of public final int age;//final modification, compilation error public final int age1 = 20; reference type modified by public final StringBuilder address;// final; compilation error public final StringBuilder address1 = new StringBuilder ("China"); / / reference type modified by final, compiled through}
So what is the difference between final variables and ordinary variables? take a look at the following example
Public static void main (String [] args) {String str0 = "helloWorldChina"; String str1 = "helloWorld"; String str3 = str1 + "China"; System.out.println (str0 = = str3); / / false final String str2 = "helloWorld"; String str4 = str2 + "China"; System.out.println (str0 = = str4); / / true final String str5; str5 = "helloWorld"; String str6 = str5 + "China"; System.out.println (str0 = = str6); / / false}
Str0 = = str3 runs as false, because a new string object is generated through "+", and the reference address returned is no longer the same as str0, as explained in "Java Basics (3) String Deep parsing".
So why is the execution result of str0 = = str4 true?
For variables modified by final, if the exact value can be known at compile time (initialized when the variable is defined), the compiler will use it as a constant, and all places where the variable is used are equivalent to using the constant directly. String str4 = str2 + "China" has been merged into String str4 = "helloWorldChina" during compilation, so str0 and str4 refer to the address of the same string literal in the constant pool. So the result is true.
And the execution result of str0 = = str6 is also easy to understand for false.
Str5 does not know the exact value at compile time, but is initialized before it is used, so the compiler cannot merge in advance, str6 generates a new string object with "+", and the reference address returned is no longer the same as str0.
However, for basic data types, there is no difference between final variables and ordinary variables.
Public static void testint () {int int0 = 8; final int int1; int1 = 4; int int2 = int1 + 4; System.out.println (int2 = = int0); / / true}
Because the concept of reference passing does not exist in the basic data type, and the basic type variable is also literally constant, the operation on the basic type is a direct operation on the value, unlike the reference, the address is not compared.
That's all for "how to use the final keyword in Java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.