Home  >  Article  >  Java  >  Interviewer: Please explain in detail the role of the final keyword and its difference from static

Interviewer: Please explain in detail the role of the final keyword and its difference from static

王林
王林forward
2021-03-08 10:27:404458browse

Interviewer: Please explain in detail the role of the final keyword and its difference from static

Preface:

As expected, the interviewer asked this question again in the last interview: Could you please tell me the specific meaning of the final keyword and explain it in detail? Explain its function and its difference from static. In order to make everyone who is interviewing pay attention to this question, the answers are specially compiled for your reference.

1. The meaning of the final keyword

The surface meaning of final is that it cannot be changed, which means constant; similar to the const keyword in C language, it means that it cannot be changed. Quantity, which is different from static scalar. Static variables refer to only one storage space and the value can be changed. The reason for using final is from the perspective of software design, because when others see the keyword final, they will know what it means and achieve the effect of understanding it. However, it is precisely because of the existence of this "semantics" that you must be cautious in programming. Use to avoid misuse.

In Java, final modifications are constants, and variable names must be capitalized;

Math class: public static final double E = 2.7182818284590452354; public static final double PI = 3.14159265358979323846;
. .....Many variables in the java source code are modified with final

2. The role of final

The role of final is different depending on the position of the modification, for three situations:

(1) Modify variables. Variables modified by final must be initialized and cannot be reassigned after the initial value is assigned.

Note: Local variables are not within the scope of our discussion, because local variables themselves have scope and are not modified with words such as private and public.

(2) Modified method, the method modified by final means that it cannot be rewritten.

(3) Modified classes. Classes modified by final cannot be inherited.

Note: For a final-modified class, all member methods in the class are implicitly designated as final methods.

2.1. Final modified variables

Variables modified by final must be initialized explicitly. Initialization can be done in three ways: 1) initialized when defined, 2) set in the constructor Value, 3) Set the value for the final instance variable in the non-static block.

Final modified variable means: this variable cannot be changed after it is initialized. The immutable meaning here means that its value is immutable for basic types, and for object variables, its reference is immutable, that is, Can no longer point to other objects.

public class Test01{    
    final int x1= 10000;    
    final int x2;    
    final int x3;
    {
       x2 = 20000;
        }
    Public exe3(){        
        this.x3 = 3000;
        }
}

If the variable modified by final is an object type, then unchangeable means that the variable cannot point to other objects, but the value of the object can be changed, for example:

final Operate operate = new Operate() ;// operate有一个普通变量i初始化为10operate.i = 11;
operate.i = 12;
System.out.println(operate.i); //输出12上述是自定义类,即便是数组,List等集合类型,所保存的值也是可以更改的。

3. The difference between final and static

static acts on member variables to indicate that only one copy is saved, while final is used to ensure that variables are immutable. Take a look at an example on the Internet:

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的值都是一样的,j是static类型的属于类,因此两次值相同。i不是static的因此属于对象,但是i的值是不可变的。

(Learning video sharing: java video tutorial)

4. Other final related knowledge

(1) Use the final keyword, if compiled If the compiler can determine the value of a variable during the compilation phase, the compiler will use the variable as a compile-time constant. If this needs to be determined at runtime, then the compiler will not optimize the relevant code.

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));
    }
}
    //final类型,在编译阶段能够确定值。
    //非final类型在编译阶段确定不了
    输出:
        true
        false
    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";
    }
}
//即便是final类型,编译阶段也确定不了值。输出false

(2) Be careful not to confuse final with finally, finalize(), etc.

(3) Declaring classes, methods, and variables as final can improve performance, so that the JVM has the opportunity to estimate and then optimize.

(4) The variables in the interface are all public static final.

Related recommendations: java interview questions and answers

Original link: http://www.cnblogs.com/liun1994/p/6691094.html

Original author: Suifenglangzi90

The above is the detailed content of Interviewer: Please explain in detail the role of the final keyword and its difference from static. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete