java-overloads, package modifiers, and Stack management

Source: Internet
Author: User
Tags modifiers


1. Override of Method (override): Rewrite, overwrite

1) occurs in parent-child class, method name is the same, parameter list is same, method body is different
2) When the override method is called, look at the type of the object
2. Overrides are different from overloading:
1) Rewrite (override):
1.1) occurs in parent-child class, method name is the same, parameter list is same, method body is different
1.2) Follow the "run-time bindings" to see the type of the object to invoke the method

(last example)


2) Overload (overload):
2.1) occurs in a class, the method name is the same, the parameter list is different, the method body is different
2.2) Follow the "compile-time binding" to see the type of reference to bind the method
3.package:
1) Function: Avoid naming conflicts of classes
2) The full name of the class: package names. class name
3) Package name can have a hierarchy, the class in the same package cannot have the same name
4) Recommendation: Package name all letters are lowercase
Import:
1) classes in the same package can be accessed directly
classes in different packages cannot be accessed directly, and there are two ways to access them:
1.1) First Import declaration class re-use----recommendations
1.2) The full name of the class----------------too cumbersome, not recommended
4. Access Control modifiers:
1) Public: Open, any class
2) Private: proprietary, this class
3) Protected: Protected, this class, sub-class, same package class
4) Default: Do not write anything, this class, the same package class
access adornments for the class: public, Default
access adornments for members in the class: 4 kinds are available
5. Memory Management:---------------------Understanding by JVM Management
1) Heap:
1.1) Store all new objects (including member variables)
1.2) No reference to the object is garbage,The garbage collector (GC) does not regularly sweep garbage in memory,

The recycling process is transparent, but it is not always recovered as soon as the garbage is found,call System.GC () to suggest that the JVM dispatch GC to collect garbage as soon as possible
1.3) Memory leaks: memory that is no longer being used has not been recovered in time
Recommendation: Objects that are no longer used set references to NULL in a timely manner
1.4) The life cycle of the member variable:
The object is created in the heap, and the object disappears when it is recycled
2) Stack:
2.1) Store all local variables (including parameters) of the method in the call
2.2) When invoking a method, allocate a corresponding stack frame to the method in the stack,all local variables (including parameters) are stored in the stack frame .at the end of the method call, the stack frame is cleared and the local variable is invalidated.
2.3) The life cycle of local variables:
The call method is present in the stack and is cleared with the stack frame at the end of the method call


3) Method Area:
3.1) Storage of. Class bytecode files (including methods, static variables)
3.2) Only one copy of the method can be used to distinguish specific objects


6.static: Static
1) Static variables:
1.1) Modified by static
1.2) belonging to the class, stored in the method area, only one copy
1.3) often accessed through the class name point
1.4) When to use: Resources shared by all objects (Pictures, audio, video, etc.)


2) static method:
2.1) Modified by static
2.2) belonging to the class, stored in the method area, only one copy
2.3) often accessed through the class name point
2.4) The static method does not have an implicit this pass,
Instance members cannot be accessed directly in a static method
2.5) When to use: the operation of the method is only relevant to the parameter and is not related to the object
3) static BLOCK:
3.1) Modified by static
3.2) belongs to the class, and is executed automatically when the class is loaded.
Because the class is loaded once, the static block is executed only once
3.3) When to use: Load/Initialize static resources (Pictures, audio, video, etc.)

/**
* Follow the principle of "two and two small and one big":
* 1) Two:
* 1.1) Method name is the same
* 1.2) Same parameter list
* 2) Two small:
* 2.1) The return value type of the subclass method is less than or equal to the parent class's
* 2.1.1) void must be the same
* 2.1.2) Basic type must be the same
* 2.1.3) reference type, less than or equal to
* 2.2) when the subclass throws an exception that is less than or equal to the--------exception of the parent class
* 3) a large:
* 3.1) The subclass method has access rights greater than or equal to the parent class's
*/

The parent class is large, the sub-class is small
Class coo{
void Show () {}
Double Test () {return 0.0;}
Doo Sayhi () {return null;}
Public Coo Say () {return null;}
}
Class Doo extends coo{
int Show () {return 1;}//Compile error, must be equal when void
int Test () {return 0;}//Compile error, basic type must be equal
Coo Sayhi () {return null;}//Compile error, reference type must be less than or equal to
Public Doo Say () {return null;}//reference type, less than
}

I am a beginner, if the update is not good, welcome the Great God pointed out, thank you!

More exciting after the update, reprinted annotated!

java-overloads, package modifiers, and Stack management

Related Article

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.