Java Basics Review: Final,static, and the String class

Source: Internet
Author: User

2.final

1) Why is the string final modified?

Answer yourself:

Answer:

Mainly for the sake of "efficiency" and "safety". If string is allowed to be inherited, the performance of the program may be reduced due to its high usage rate, so string is defined as final.

There is another explanation:

A class with a final modifier is not derived. In the Java Core API, there are many examples of final applications, such as java.lang.String. Specifying final for the string class prevents people from overwriting the length () method.

In addition, if you specify a class to be final, all methods of that class are final. The Java compiler looks for the opportunity Inline (inline) for all final methods (this is related to the specific compiler implementation). This will increase the performance by an average of 50%.

The String class.

Efficiency, safety, if it can be inherited impact performance

The final class method is final, preventing its

method is overridden, such as length (), and the Java inline final method improves performance.

3.final

You can modify the class, modify the method, modify the variable name, (think about how the map will be decorated,? )

1) When a class is decorated with final, it indicates that the class cannot be inherited. (with security)

2) Modification method: prohibits the method from being overwritten in subclasses. (formerly also for efficiency)

Note: The private method is implicitly defined as final. Is that private methods are not overwritten

3) modifier variable: If it is a basic numeric type, then the assignment cannot be modified.

If it is a reference type, then it can no longer point to other objects after it is initialized, such as cannot be final A = new A, no more a = new A;

4) The final variable differs from the normal member variable:

5) The difference between static and final:

Static acts on member variables to indicate that only one copy is saved, and final is used to guarantee that the variable is immutable. (but will guarantee multiple)

Is two things, static will be typed, and final will not, Static is as long as a

6) A map of final retouching

You can modify the data, because this is a modified reference.

such as final map map =new HashMap (); You can put data into a map

Final List List =new ArrayList (); You can add data to the list.

So if final map map =new HashMap (); ,

Then write map =new HashMap (); It's not going to work.

Final class is not inherited, security;

The final method is not overwritten, and the private method defaults to final;

The final variable can remain unchanged and not be re-assigned, and if the reference is guaranteed not to point to other objects.

The compiler considers the final value as quantitative

Note that if you do not assign a value directly, the Get method

This can also be false.

Static saves only one copy, and final is guaranteed to be invariant

Modifier map,list, equivalent to a variable that modifies a reference type.

4.static

1) A decorated class, which can only be an inner class, at which time the inner class is used as a normal class; Inner class?

2) static method: Is the method without this. A non-static method cannot be called inside a static method, which in turn is possible. It is also possible to invoke the static method only through the class itself, without creating any objects. This is actually the primary use of the static method.

3) Static variable: Only one copy is saved in memory, and is initialized only once when the class is loaded. Instead of static variables are initialized when the object is created, multiple replicas

4) Static code block: Initialized this time only when the class is loaded, which can be used to improve performance

Questions:

1) does static change the access rights of members?

No, in Java, access permissions are private,public,protected

2) can I access static member variables through this?

Can.

The result is 33, because the static modifier's value is shared by all objects. (Note: Java specifies that there are no static modified local variables.) So this is the value of the main class, which is naturally 33 of the static modifier.

3) Execution order problem:

Static code block for base static parent class
Static code block for test static subclass
How to construct base constructor parent class
How to construct the test constructor sub-class

------------------------------------------------------

Test static
MyClass static
Person static
Person Test is a little harder. This is the method of the person class
Test constructor the parent class
Person MyClass
MyClass constructor sub-class

Title Source Document

>

Modify the inner class;

The modification method, without this, can invoke the static adornment method directly through the class itself without creating the object;

Modifier variable, only one copy, class loading time initialization, is shared by all objects.

Static blocks of code that are executed first, the child parent, the parent code block, the child code block, and the other

Java Basics Review: Final,static, and the String class

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.