Learning Java programming ideology version 4

Source: Internet
Author: User

Learning Java programming ideology version 4

(1) In a class, initialization of all member variables during definition is completed before the constructor is initialized, that is, initialization during definition first, then initialize the constructor.

(2) Why is method overloading required?
1. Different types of objects may have similar operations. In the abstract process, "similar" is abstracted into the same method name, therefore, method overloading allows methods of the same name to accept different types and quantities of parameters, so as to control the behavior of this operation;
2. Method Overloading is required for Constructor methods. Because the constructor must have the same name as the class, and the class members must be initialized to different degrees (reflected in the type and quantity of parameters of the constructor), the constructor must be overloaded.

(3) How to Determine Method overloading?
Method Overloading is based on "method name", "method parameter", and "method parameter order". Method overloading must meet the following conditions:
1. You can only use different parameter styles when using overload. For example, different parameter types, different parameter numbers, and different parameter Order (of course, several parameter types in the same method must be different, such as fun (INT, float ), but cannot be fun (INT, INT ));
2. You cannot use the "access permission", "return type", or "thrown exception" method to overload it;
3. The method exception type and quantity do not affect the heavy load;
4. For inheritance, if a method has a priavte access permission in the parent class, it cannot be overloaded in the subclass. If it is defined, it only defines a new method and does not achieve the effect of overloading.

(4) What is the impact of the basic type of method parameters on the specific content?
Because the basic type is automatically upgraded from a small type to a large type, if the input int, but the class only has F (long) and F (float ), because there is no F (INT), int is automatically upgraded to long, and then f (long) is called. The order of improvement is byte-Char-short-int-Long-float-double.

(5) What is the ambiguity of heavy load?
If all the others are the same during method overloading, but the return types are different, there will be ambiguity, such as F () {}and int F () {return 1 ;}.
1. If int I = f (); is called, the compiler can find the called method correctly;
2. If only F (); is called, the compiler does not have any conditions to determine which method should be called;
Therefore, it cannot be overloaded Based on the return type.

(6) Why must the local variables defined in the method body be explicitly initialized?
For the basic type of Java, if Explicit initialization is not performed when defining the class member variables, the initialization is implicitly set to 0 or false. However, display initialization must be performed when defining a method body, because the compiler will think that this is a "careless error" of the programmer. It is possible that he does not need to define or define this type, explicit initialization will prompt programmers.

(7) When constructing an instance of a class, the compiler mainly performs the following three tasks in sequence:
1. allocate memory space for objects;
2. initialize the value of the instance variable in the object. The initialization value can be the default value or be initialized as specified. The "specified method" may be direct assignment, expression assignment, or calling any other method assignment;
3. Call the object construction method.

(8) A static variable is initialized only when necessary. It is initialized only when used for the first time and is always available for other instances. "When necessary" refers to: when an object is created, the class name is used for direct access, and the class is loaded.

(9) constructor is actually a static method even if the static keyword is not explicitly used.

(10) Java initialization sequence
When a Java class is loaded for the first time, it initializes static member variables or methods, but the method is not executed if it is not called. The static member variables and static initialization block levels are the same, non-static member variables are of the same level as non-static initialization blocks.
Initialize the static code of the parent class first ---> initialize the static code of the subclass --> (if no instance is created during instance creation, the subsequent code will not be executed) initialize the non-static code of the parent class ---> initialize the parent class constructor ---> initialize the non-static Code of the Child class ---> initialize the Child class Constructor
Other Instructions:
1. Classes are loaded by the Java class loader only when they are created using the new call;
2. When creating a class instance, initialize it according to the parent-child inheritance relationship;
3. When a class instance is created, the initialization block part is first executed, followed by the constructor; then, the initialization block of the subclass inherited from this class is executed, and finally the constructor of the subclass is executed;
4. When the class is eliminated, first the subclass part is eliminated, and then the parent class part is eliminated;

(11) Static block initialization: the static block is as fast as other static initialization, only once: when an object is created for the first time or when a static member of the class is called for the first time (usually a static member of the class name is used directly ).
Non-static block initialization: 1. provides direct support for anonymous class initialization; 2. ensures that specific initialization operations are performed no matter which constructor is called.

(12) Class access levels include package and public, and private and protected.
1. For a package-level class, the method is generally set to package, but it can also be set to public.
2. In fact, the access level of the internal class can be private and protected, but this is the only special case.

(13) Final variables are divided into two types: initialization at compilation (constants of this type must be of the basic type) and runtime initialization.
Final makes the value of a variable of the basic type a constant, and the reference variable of an instance of the Object Type A constant. It cannot reference other instances, however, the instance it references can be changed.

(14) What is blank final? What is the function?
The member variables are defined as final, but are not explicitly initialized for the moment. But it must be initialized before use, and the compiler will check. Note: Implicit initialization of variables of the basic type defined as final does not work.
Variables defined as blank final are generally initialized in the constructor, so some special effects can be produced when you create an instance of the class: make the instance variables of different instances of each class have different values (to achieve this effect, you cannot add the static keyword ).

(15) Final parameters: Apply final restrictions to method parameters. They are mainly used to pass data in an anonymous internal class.

(16) Methods of the final type: first, adding final to the method defined by the parent class can prevent the quilt class from overwriting. Second, efficiency was taken into consideration in the early days. Now, this is no longer the case.

(17) All private methods of the class are implicitly final and do not need to be explicitly declared as final. However, private member variables are not final. Because private member variables can be implicitly initialized, final variables cannot be initialized through implicit initialization. Therefore, if private member variables are final, the final keyword must be explicitly added.

(18) classes of the final type cannot be inherited. The member variables of the final class can be final or not final. You can decide on your own. However, all methods of the final class are implicitly final.

(19) In a class, if the member variable or method is defined as private, it indicates that the member variable or method is not the interface provided by the class to the outside. Overwriting only occurs on the interface provided by the class to external users. Therefore, if the subclass defines a method with the same name and parameter as the private member variable of the parent class, it does not overwrite the method, it is a new method defined by the subclass.

(20) method coverage and method overloading share the following similarities:
1. The method must have the same name.
2. Can be used between abstract methods and non-abstract methods.
3. Method coverage and method overloading have the following differences:
4. Method override requires that the Parameter Signatures be consistent, while method overloading requires that the Parameter Signatures be inconsistent.
5. Method override requires that the return types be consistent, while method overloading does not limit this.
6. Method override can only be used for sub-classes to overwrite the methods of the parent class. Method Overloading is used for all methods of the same class (including methods inherited from the parent class ).
7. Method override has special requirements on method access permissions and thrown exceptions, and method overloading has no restrictions in this regard.
8. A method of the parent class can only be overwritten once by the quilt class, and a method can be reloaded multiple times in the class.
Pay attention to the following points when overwriting:
1. the logo of the covered method must match the logo of the covered method to achieve the coverage effect;
2. The return type of the overwritten method must be the same as the return type of the overwritten method. (jdk1.4 and earlier versions have this strict requirement, which has been weakened since jdk1.5, only the return type of the override method must be the same as the return type of the overwritten method, or the former is the subclass of the latter)
3. The exception thrown by the overwriting method must be the same as that thrown by the overwriting method, or its subclass;
4. The method to be overwritten cannot be private. Otherwise, a new method is defined in its subclass and not overwritten.
5. The access level of the subclass method cannot be lower than that of the parent method.

(21) Only class methods have polymorphism; class member variables do not have polymorphism; Class static methods do not have polymorphism.
If the parent class and subclass have member variables of the same name, they are not covered, but hidden. The subclass actually contains these two variables, their buckets are different, but this variable of the subclass is the default. To access the variable of the parent class, you must explicitly use super for access.

(22) an abstract class may not contain abstract methods. At this time, it only serves to make it impossible to create instances of this class;

(23) The interface can contain member variables, both of which are static and final, but not blank final (static conflicts with blank final, refer to (14th )), if the interface class does not explicitly set the access level, the default value is package. If the method in the interface does not explicitly set the access level, the default value is public, rather than the usual default value is package.

 

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.