On the basis of Java (v) final keyword

In front explain String referred to the final keyword, keyword final paper will be resolved.

static and final are the two we must master keyword. Unlike other keywords, they have a variety of uses, but also used under certain circumstances, can improve the operating performance of a program to optimize the structure of the program. Let's look at the final keywords and their usage.

final is on the whole, "immutable", it can be used to modify the class, method, variable.

A. Final class

final modified class, which can not be inherited. When you confirm that a class will never be inherited or do not want to be inherited, it can be modified with final.
Here Insert Picture Description

Similarly, for the interface (interface) and abstract classes (abstract Class), which is to this "multi-state" designed, you can not use the keyword final modification
Here Insert Picture DescriptionHere Insert Picture Description

Method final class members in the final default method also implicitly designated.

Two. Final method

The final modification methods can not be overridden.

example:

/**
 * 父类
 * @author LH
 */
public class FinalDemo1 {
    public final void test() {
        
    }
}

Here Insert Picture Description

Three. Final variable

final variables include member variables and local variables. Variable types include the basic data types, object.

By modifying final basic types of local variables (and its packaging), a value initialized (initialization can be defined, can be initialized before use) can not be changed. Such as:

final int a = 0;
a = 1;//报错
final int b;
b = 1;//编译通过

When reference type variable final modification by topical, which references an object (the memory address) (initialization can be defined, can be initialized before use) can not be changed, but the data stored in the object may vary

public static void main(String[] args) {
	final String str1 = "helloWorld";
	str1 = "helloChina";//编译出错,String的不可变性,此处返回的是新的对象引用。

	final StringBuilder sb = new StringBuilder("hello");
	sb.append("world");//编译通过

	sb = new StringBuilder("China");//编译出错
}

final member variables must be modified directly initialized when defined, otherwise it will compile error

public class FinalDemo1 {
	public final int age;//final修饰的基本类型,编译出错
	public final int age1 = 20;//final修饰的基本类型,编译通过
    public final StringBuilder address;// final修饰的引用类型,编译出错
    public final StringBuilder address1 = new StringBuilder("中国");//final修饰的引用类型,编译通过
}

Example So in the end what is the difference between the final and a regular variable, see below

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 operation result is false, because by "+" to generate a new string object that references the return address and str0 never be the same, in which " the Java basis (three) String depth analysis has to explain" in .

So why the results str0 == str4 is true?

Modified by the final variable, if you can know the exact value at compile time (when he defined variable initialization), then the compiler will use it as a constant, all local variables used in the direct equivalent of the constant use, string str4 = str2 + "China" have been merged during the compilation process into string str4 = "helloWorldChina", so the same references str4 str0 with a literal string constant pool of addresses, and therefore the result is true.

The results of the implementation str0 == str6 is false is also well understood

str5 at compile time did not know the exact value, but only be initialized before use, so the compiler can not advance the merger process, str6 by "+" to generate a new string object and returns a reference to the address and not str0 again the same.

And for basic data types, is defined as final regular variable, the comparison result is no difference

public static void testint(){
	int int0 = 8;    
	final int int1;    
	int1 = 4;    
	int int2 = int1 + 4;    
	System.out.println(int2 == int0);//true
}

Because there is not only the basic data types passed by reference, the basic types of variables are literal, so the basic type of operation is a direct operation on the values, and the reference is not the same, not the address comparison.

IV. Summary

This paper focuses on the principle of final keywords were explained, and describes their basic usage, including the final modified class, final modification methods and final modification of variables, in addition to the text String variables == comparison simply to be more clearly Description final principle, the practical application scenario comparison with the time or equals () method, final and static are often used in conjunction with a "global constant", if imperfect, please criticism and hope to make progress together, thank you!

Guess you like

Origin blog.csdn.net/fengdongsuixin/article/details/93377946