Parsing of the Java final keyword

Source: Internet
Author: User

The final keyword gives the most obvious is fixed, can be modified class, method, variable. after modification, the class is not inheritable, the method is not overwritten, and the variable is immutable.

Note: The private method of the class is implicitly specified as the final method.

For a final variable, if it is a variable of the base data type, its value cannot be changed once initialized, and if it is a variable of a reference type, it cannot be pointed to another object after it has been initialized.

After understanding the basic usage of the final keyword, let's look at where the final keyword is easy to confuse.

1. What is the difference between a class's final variable and a normal variable?

When you use final action on a member variable of a class, the member variable (note is the member variable of the class, the local variable needs to be guaranteed to be initialized before use) must be assigned in the definition or constructor, and once the final variable is initialized, it can no longer be assigned a value.

Let's look at an example:

publicclassTest {

     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));      } }Output Result:
TrueFalse

We can first think about the output of this problem. Why the first comparison result is true, and the second compares the result to Fasle. This is the difference between the final variable and the normal variable, when the final variable is the base data type and the string type, if you can know its exact value during compilation, the compiler will use it as a compile-time constant. That is, where the final variable is used, it is equivalent to the constant that is accessed directly, and does not need to be determined at run time. This is a bit like a macro substitution in C language. So in the preceding section of code, because the variable B is final decorated, it is treated as a compiler constant, so where B is used, the variable b is replaced directly with its value. The access to variable D needs to be linked at run time. Presumably the difference should be understood, but note that the compiler does this only if the final variable value is known exactly during compilation, as the following code does not optimize:

publicclassTest {    publicstaticvoidmain(String[] args)  {        String a = "hello2"        finalString b = getHello();        String c = b + 2        System.out.println((a == c));    }        publicstaticString getHello() {        return "hello";    }}

The output of this code is false.

2. Is the content of the object pointed to by the final modified reference variable variable?

The point of a final modified variable cannot be changed, and the content can be changed.

Parsing of the Java final keyword

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.