Speaking of the final keyword, many people must be familiar with it. The final keyword may be often used when using anonymous inner classes. In addition, the String class in Java is a final class, so today we will learn about the usage of the final keyword.
Main introduction: 1. Basic usage of final keyword. 2. Deeply understand the final keyword
1. Basic usage of final keyword
In Java, the final keyword can be used to modify classes, methods and variables (including member variables and local variables). Let’s learn about the basic usage of the final keyword from these three aspects.
1. Modification class
When a class is modified with final, it indicates that this class cannot be inherited. In other words, if you will never let a class be inherited, you can modify it with final. Member variables in a final class can be made final as needed, but be aware that all member methods in a final class will be implicitly designated as final methods.
When using final to modify a class, you should choose carefully. Unless this class really will not be used for inheritance in the future or for security reasons, try not to design the class as a final class.
2. Modification methods
The following passage is excerpted from page 143 of the fourth edition of "Java Programming Thoughts":
"There are two reasons for using final methods. The first reason is to lock the method to prevent any inherited class from modifying its meaning; the second reason is efficiency. In early Java implementation versions, final Methods are converted to inline calls. However, if the method is too large, you may not see any performance improvement from inline calls. In recent Java versions, there is no need to use final methods for these optimizations. "
Therefore, set a method to final only if you want to explicitly prevent the method from being overridden in subclasses.
Note: Private methods of a class are implicitly designated as final methods.
3. Modify variables
Modifying variables is the most commonly used place for final, and it is also what this article will focus on next. First, let’s understand the basic syntax of final variables:
For a final variable, if it is a variable of a basic data type, its value cannot be changed once it is initialized; if it is a variable of a reference type, it cannot point to another object after it is initialized.
For example:
In the above piece of code, errors are reported when reassigning variables i and obj.
2. In-depth understanding of final keywords
After understanding the basic usage of the final keyword, in this section we will take a look at the areas where the final keyword is easily confused.
1. What is the difference between final variables and ordinary variables of a class?
When using final to act on a member variable of a class, the member variable (note that it is a member variable of the class, local variables only need to ensure that they are initialized and assigned before use) must be initialized and assigned at the time of definition or in the constructor. Moreover, once a final variable is initialized and assigned a value, it cannot be assigned a value again.
So what is the difference between final variables and ordinary variables? Please see an example below:
public class Test { public static void main(String[] args) { String a = "hello2"; final String b = "hello"; String d = "hello"; String c = b + 2; String e = d + 2; System.out.println((a == c)); System.out.println((a == e)); } }
true
false
You can first think about the output result of this question. Why the first comparison result is true and the second comparison result is fasle.
This is the difference between final variables and ordinary variables. When the final variable is a basic data type or String type, if its exact value can be known during compilation, the compiler will use it as a compile-time constant. That is to say, where the final variable is used, it is equivalent to direct access to the constant and does not need to be determined at runtime. This is somewhat similar to macro substitution in C language.
So in the above piece of code, since the variable b is modified as final, it will be treated as a compiler constant, so where b is used, the variable b will be directly replaced with its value. However, access to variable d needs to be done through the link at runtime.
I think everyone should understand the difference, but please note that the compiler will only perform such optimization if the value of the final variable can be accurately known during compilation. For example, the following code will not be optimized :
public class Test { public static void main(String[] args) { String a = "hello2"; final String b = getHello(); String c = b + 2; System.out.println((a == c)); } public static String getHello() { return "hello"; } }
The output result of this code is false.
2. Is the content of the object pointed to by the reference variable modified by final mutable?
As mentioned above, the reference variable modified by final cannot point to other objects once it is initialized and assigned. So, is the content of the object pointed to by the reference variable mutable? Look at this example:
public class Test { public static void main(String[] args) { final MyClass myClass = new MyClass(); System.out.println(++myClass.i); } } class MyClass { public int i = 0; }
这段代码可以顺利编译通过并且有输出结果,输出结果为1。这说明引用变量被final修饰之后,虽然不能再指向其他对象,但是它指向的对象的内容是可变的。
3.final和static
很多时候会容易把static和final关键字混淆,static作用于成员变量用来表示只保存一份副本,而final的作用是用来保证变量不可变。看下面这个例子:
public class Test { public static void main(String[] args) { MyClass myClass1 = new MyClass(); MyClass myClass2 = new MyClass(); System.out.println(myClass1.i); System.out.println(myClass1.j); System.out.println(myClass2.i); System.out.println(myClass2.j); } } class MyClass { public final double i = Math.random(); public static double j = Math.random(); }
运行这段代码就会发现,每次打印的两个j值都是一样的,而i的值却是不同的。从这里就可以知道final和static变量的区别了。
4.匿名内部类中使用的外部局部变量为什么只能是final变量?
这个问题请参见上一篇博文中《Java内部类详解》中的解释,在此处不再赘述。
5.关于final参数的问题
关于网上流传的”当你在方法中不需要改变作为参数的对象变量时,明确使用final进行声明,会防止你无意的修改而影响到调用方法外的变量“这句话,我个人理解这样说是不恰当的。
因为无论参数是基本数据类型的变量还是引用类型的变量,使用final声明都不会达到上面所说的效果。
看这个例子就清楚了:
上面这段代码好像让人觉得用final修饰之后,就不能在方法中更改变量i的值了。殊不知,方法changeValue和main方法中的变量i根本就不是一个变量,因为java参数传递采用的是值传递,对于基本类型的变量,相当于直接将变量进行了拷贝。所以即使没有final修饰的情况下,在方法内部改变了变量i的值也不会影响方法外的i。
再看下面这段代码:
public class Test { public static void main(String[] args) { MyClass myClass = new MyClass(); StringBuffer buffer = new StringBuffer("hello"); myClass.changeValue(buffer); System.out.println(buffer.toString()); } } class MyClass { void changeValue(final StringBuffer buffer) { buffer.append("world"); } }
运行这段代码就会发现输出结果为 helloworld。很显然,用final进行修饰并没有阻止在changeValue中改变buffer指向的对象的内容。
有人说假如把final去掉了,万一在changeValue中让buffer指向了其他对象怎么办。有这种想法的朋友可以自己动手写代码试一下这样的结果是什么,如果把final去掉了,然后在changeValue中让buffer指向了其他对象,也不会影响到main方法中的buffer,原因在于java采用的是值传递,对于引用变量,传递的是引用的值,也就是说让实参和形参同时指向了同一个对象,因此让形参重新指向另一个对象对实参并没有任何影响。
以上内容就是Java中final关键字详解,希望对大家有所帮助。

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software