Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use inal keyword of Java concurrency

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to use the inal keyword of Java concurrency". In the daily operation, I believe that many people have doubts about how to use the inal keyword of Java concurrency. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to use the inal keyword of Java concurrency". Next, please follow the editor to study!

1. Final usage scenarios

Final can dilute variables, methods, and classes, and the modified content will not be changed once assigned. For example, the string class is a class of type final.

Second, final modifies variables

In Java, variables can be divided into member variables and local variables.

Member variable

Usually, member variables in each class can be divided into class variables (static-modified variables) and instance variables assign initial values to these two types of variables at different times.

Class variables can directly assign initial values to class variables when they are re-declared or assign initial values to class variables in static code blocks, while instance variables can assign initial values to instance variables when declaring variables. assign initial values in non-static initialization blocks and constructors.

Class variables have two times to assign initial values, while instance variables can have three times to assign initial values. When the final variable is not initialized, the system will not initialize implicitly and an error will occur.

Summarize and sort out these situations:

Class variables: initial values must be specified in static initialization blocks or when such variables are declared, and can only be alerted in one of these two places

Instance variable: the instance variable must be declared in a non-static initialization block, or an initial value must be specified in the constructor, and can only be alerted in these three places.

Local variable

The final local variable is initialized by the programmer. If the final local variable has been initialized, it cannot be changed again. If the final variable is not initialized, it can be assigned once and only once. Once the value is assigned again, there will be an error.

Basic data types and reference data types

Is there any difference between final-decorated basic data types and reference types?

We have seen from the above example that if final modifies data of a basic data type that cannot be changed once assigned, then what if final modifies a reference data type? Can the referenced object be changed?

Package passtra; public class FinalExample {/ / assign private final static Person person=new Person (25,175) when declaring final instance member variables; public static void main (String [] args) {/ / A change to final reference data type person person.age=22; System.err.println (person.toString ());} static class Person {private int age; private int height; public Person (int age, int height) {super (); this.age = age This.height = height;} @ Override public String toString () {return "Person [age=" + age + ", height=" + height + "]";}

When we change the attribute of the application data type variable person modified by final to 22, it can be operated successfully.

As can be seen from the code, when final modifies the basic data type, it cannot reassign the basic data type variable, so the basic data type cannot be changed, while for the reference data type, it only saves a reference. Final only ensures that the address referenced by the reference type will not change, that is, the object will be referenced all the time, but the object property can be changed.

Macro variable

Taking advantage of the immutability of the final variable, the variable becomes a macro variable, that is, a constant, under the following three conditions of Mazu.

With the final modifier, the initial value is specified when the final variable is defined, which can be uniquely specified at compile time.

Note:

When the macro variable is used elsewhere in the program, the compiler directly replaces the value of the variable.

Third, final modification method

Rewrite

When the method of the parent class is modified by final, the subclass cannot override the method of the parent class. If you getClass () method in object that means final modified, we cannot rewrite the method, but hasdhCode () is not modified by final, we can rewrite hashCode () method.

Heavy load

Package passtra; public class FinalExample {public final void test () {} / / No error public final void test (int I) {}}

You can see that methods modified by final can be overloaded.

4. Final modified class

When a class is modified by final, it indicates that the class cannot be inherited by a subclass.

Subclasses can often override the methods and properties of the parent class, which brings some security risks, so when a class does not want to be inherited, it can be modified with final.

What are the applications of final in JDK

Final is often used as an immutable class, taking advantage of the immutability of final.

Invariant class

An immutable class means that after an instance of this class is created, the instance variable of that instance cannot be changed. You can become an immutable class if the following conditions are met:

Use private and final to modify the member variables of this class

Provides a constructor with parameters to initialize the member variables of a class

Only the getter method is provided for the member variables of this class, not the setter method, because ordinary methods cannot modify final-modified member variables.

Overriding the hashCode and equals methods of object if necessary should ensure that the HashCode values of the same two objects are determined by equals to be equal.

The eight wrapper classes and String classes provided in JDK are immutable, so take a look at the implementation of string

Private final char value []

It can be seen that the value of string is modified by final, and the other properties mentioned above are also consistent.

At this point, the study on "how to use the inal keyword of Java concurrency" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report