java-Foundation (i)

Source: Internet
Author: User

1. A variable can be declared final, which is intended to prevent its contents from being modified. This means that when declaring the final variable, you must initialize it (in this usage, final is similar to the const in C + +).

For example:

final int file_new = 1;

final int file_open = 2;

Final int file_save = 3;

Final int file_saveas = 4;

Final int file_quit = 5;

The subsequent parts of your program can now use File_open and so on, as if they were constants, without worrying about their values being changed. Selecting uppercase for all the characters of the final variable is a common coding convention. A variable that is declared final does not occupy memory in the instance. Thus, a final variable is essentially a constant. The keyword final can also be applied to a method, but its meaning and its use for variables are essentially different.

2. Introduction to nested classes and inner classes [personal understanding is primarily used to parse JSON data structures]

A class defined in another class is a nested class (nested classes). The scope of a nested class is limited by the scope of the class into which it is loaded. Thus, if Class B is defined within the Class A, then B is known as a, but not by the outside of a. A nested class can access members of a class that is nested within it, including private members. However, the enclosing class cannot access the members of the nested class. There are generally 2 types of nested classes: those that precede the static identifier and the non-static identifier. A static nested class has a static modifier. Because it is static, it can only be accessed by objects that surround the members of the class. That is, it cannot directly refer to a member of a class that encloses it. Because of this limitation, static nested classes are seldom used. The most important type of nested classes is the inner class (inner class). The inner class is a non-static nested class. It can access all the variables and methods of its outer class, and it can reference them directly, just like the other non-static members in the outer class. Thus, an inner class is completely within the bounds of its enclosing class.

3. Use final in inheritance (like sealed class seal, cannot be rewritten)

The final keyword has three uses. First, it can be used to create an equivalent of a named constant. This usage is described in the previous chapters. The other two usages of final are applied to inheritance, both of which are described below. 8.8.1 using final block overloading although method overloading is one of the most powerful features of Java, sometimes you want to prevent it from happening. The method is not accepted for overloading, and the final modifier is defined before the method. A method declared as final cannot be overloaded. The following program section illustrates

The final usage:

Class A

{

final void Meth ()

{System.out.println ("This is a final method.");

} class B extends A {void meth () {//error! Can ' t override. System.out.println ("illegal!"); } }

Because meth () is declared final, it cannot be overloaded with B, and if you try to do this, a compile-time error will be generated. The final method can sometimes improve the performance of the program: The compiler is free to call the final method inline because it knows that these methods cannot be overridden by subclasses. When a small final function is called, it is common for the Java compiler to back up the subroutine's bytecode directly by invoking the compiled code of the method, thus reducing the expensive overhead associated with the method invocation. Inline is only an option for the final method. Typically, Java calls methods dynamically at run time, which is called late binding (late binding). However, since the final method cannot be overloaded, the invocation of the method can be resolved at compile time, which is called early binding (early binding).

Using final block Inheritance Sometimes you want to prevent a class from being inherited. This is done only by adding final before the class declaration. Declaring a final class implicitly declares that all its methods are final. You might think that declaring a class that is both abstract and final is illegal because the abstract class itself is incomplete, and it relies on its subclasses to provide a complete implementation. The following is an example of a final class: final class A {//...}//The following class is illegal. Class B extends A {//error! Can ' t subclass A//...} As the note implies, B inherits a is not legal, because a is declared final.

The package in 4.java is similar to namespace in C # The role of a bundle like an assembly

Import Pk1.pk11.classname->using Namespace.subnamespace.classname

The 5.interface default modifier is: public

6. Inherit extends implementation interface: Implents

7. Partial implementation If a class contains an interface but does not fully implement the method of the interface definition, then the class must be defined as an abstract type

Abstract class Incomplete implements Callback {int A, b; void Show () {System.out.println (A + "" + b);} // ... }

Here, class incomplete does not implement the callback () method and must be defined as an abstract class. Any class that inherits incomplete must implement the callback () method or it itself is defined as an abstract class.

8.Java built-in exception

9.instanceof type judgment

The native method of 10.JAVA, somewhat similar to the C # external method, "native method writing is implemented using JNI, which differs from a specific platform"

java-Foundation (i)

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.