The road to the restoration of Java

Source: Internet
Author: User

First, memory management

The declaration in Java is a sub-reference and a basic data type.

Array:

An array of new objects in Java is person[] A; A = new Person[4]; person[0] = new Person ();

the first sentence declares 1 A-array reference variable, pointing to null. The second sentence lets a point to an array of length 4 memory, the array element type is the person reference type, the value is null, and there is no memory allocated at this time. All have a third sentence, the third sentence is the true meaning of the allocation of memory.

The new multidimensional array in Java is int[][] A;  A = new int[4][]; A[0] = new INT[4];

The first sentence declares a two-dimensional array reference variable, pointing to null. The second sentence declares that a points to an array memory of length 4, the array element is a one-dimensional array reference type, and the value is null, at which time no memory is allocated. The third sentence assigns a one-dimensional array of length 4, because the array element is int, so the variable is automatically filled to 0. (If you change int to person, it's not enough here, and you need to continue new to allocate memory for each person).

instance variables and class variables:

A class variable is modified by static. There is a class variable in each class, and the class variable starts with memory. So the static method can access the static variable, but accessing the normal variable must be new because the normal variable does not allocate memory.

For class variables, the system allocates memory space first and assigns the base data type to 0 or null, and then initializes it sequentially, based on the order of initialization.

Memory control for parent-child instances: Methods are overwritten and variables are not overwritten. This is where the polymorphic problem is involved. When a parent class references a reference to a subclass variable, the method is invoked when it is called by the Reference. Method (), the actual type of method (subclass method) is called, and when the variable is invoked with the reference. Variable, called the method of the declaring type (parent class method).

Final modifier:

In many cases, the final modified variable is treated as a macro substitution. Subclasses cannot override the final method if they have access to the final method of the parent class.

Inner class: member inner class, Static inner (nested) class, Method inner class, anonymous inner class.

member Inner class:

Class Outer {class inner{}} compiling the code above produces two files: Outer.class and Outer$inner.class.method Inner class:Put the class in the method. (1), the method inner class can only be instantiated within a method that defines the inner class, and it cannot be instantiated outside this method. (2), the method inner class object cannot use the non-final local variable of the method in which the inner class resides. Class Outer {public void dosomething () {Finalint a =10;class inner{public void Seeouter () {System.out.println (a);}}inner in = new Inner (); In.seeouter ();} public static void Main (string[] args) {Outer out = new Outer (); out.dosomething ();}}anonymous inner class:When an anonymous inner class is used in a program, an object of that class is often created directly where the anonymous inner class is defined. The declaration format of the anonymous inner class is as follows: New ParentName () {...//internal class definition} very handy, implement an interface or a class directly at new.static inner class:Static or non-static members can be defined in a statically intrinsic class. The static meaning is that the inner class can be like other static members,It can also be accessed when there is no external class object. Static nested classes can only access static members and methods of external classes. Class Outer{static class Inner{}}class Test {public static void main (string[] args) {Outer.Inner n = new Outer.Inner ();}} The inner class defined in the static method is also staticnested class, at this time cannot add the static keyword in front of the class, the Staticnested class in the static method is similar to the application of the inner class in the normal method, In addition to the direct access to static member variables in the outer class, it is possible to access local variables in the statically method, but the local variable must be added to the final modifier before it.

The road to the restoration of Java

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.