Third, the creation and destruction of Java

Source: Internet
Author: User

In macro terms, there are two problems that no language can avoid: initialization and cleanup

First, initialize

1. Initialization in Java is tied to creating objects

The first thing to make clear is that the more common type in Java is the reference type, although each Java book introduces eight basic data types first, but these eight are actually special cases.

If a value belongs to a class itself, it is called a member variable, and if it is defined in a method, it is called a local variable. With regard to member variables,java automatically initializes its member variables when new objects are created (this is for security reasons, avoiding the programmer forgetting to initialize when creating objects) .

The automatic initialization rule is: (when the array is introduced, the array is automatically initialized, and the following rules are followed)
Short,byte,int, long:0
float,double:0.0
Char: space
Boolean:false
Reference type: null

In fact, automatic initialization occurs even before the constructor is assigned, such as:

1  Public classTest {2      Public Static voidMain (string[] args) {3A A =NewA (1);4     }5 }6 7 classA {8     inti;9      PublicA () {}Ten      PublicAinti) { One          This. i =i; A     } -}

The above simple code, at the time of new A, first performs the automatic initialization I assigns the value to 0, then executes the this.i=i, the value of I becomes 1.

2. Function overloading

In the small example above, the A () and a (int i) methods form overloads, and here's a little bit: how to differentiate Overloads

The number or type of arguments (different order can also be used as an overloaded distinction, but not recommended)
----- Note Two points: cannot differentiate overloads according to the return value, cannot be differentiated according to the privilege protected public;
This place is understood: because you invoke the method, it is necessary to be able to determine the method before the execution of the method, Java is the same, "must be executed before" this decision is not able to determine the overload based on the return value, because the return value can be obtained, the method is finished;

3. With regard to constructors, the main point is to raise two points:

-----To customize a class, it is best to explicitly define its non-parametric construction method;

-----This can be used to call a constructor method

The normal method can not call the construction method through this, only the construction method inside;
A constructor method can only be used once to invoke the constructor method;
This call constructs a method that must be written in the first sentence of the constructor method that invokes this;

4. Initialization involving static: Remember one thing, priority static

1  Public classtest{2     StaticBowl Bowl =NewBowl (1);3 Test () {4BOWL.F1 (1);5BOWL2.F1 (2);//Note that BOWL2 's creation statement is written after test ()6     }7     StaticBowl BOWL2 =NewBowl (2);8     9      Public Static voidMain (String[]args) {TenTest T =NewTest (); One     } A } -  - classBowl { theBowl (inti) { -System.out.println ("Bowl:" +i); -     } -     voidF1 (inti) { +System.out.println ("F1:" +i); -     } +}

The static data in the object is executed before the construction method, so when the test () method is called above, the BOWL2 is already executed, so although the BOWL2 new statement is written later, there is no problem with compiling and running. If it is non-static, the compilation will error.

See an example of static again:

1  Public classtest{2     Static intI=1;3     voidnor () {4System.out.println ("Normal method execution:" +i);5     }6     Static voidf () {7i++;8System.out.println ("static method Execution" +i);9     }Ten Test () { Onei++; ASystem.out.println ("Construction Method Execution:" +i); -     } -      Public Static voidMain (String[]args) { the System.out.println (TEST.I); - test.f (); -         NewTest (). nor (); -     } +}

The results are as follows:

 

The first line result is 1, because only executes test.i, can know the value during compiling, Java does not need to load test.class, so at this time I is still 1;
The second line calls F (), although it is static, but still needs to load test.class, so I becomes 2;
The third line executes the ordinary method, must build the object first, needs to load test.class,i becomes 3;
Also, static variables are initialized only once

5. Array initialization

1) Note here the three-way format for array initialization (interview FAQ)

Int[] A = {n/a};
Int[] A = new int[]{1,2,3};
Int[] A = new int[3]; a[0]= 1,a[1]= 2,a[2]=3; Be careful not to add (), that is, new int[3]

2) Here's a list of mutable parameters (seemingly less likely to see someone use it), see the following example:

1  Public classtest{2      Public Static voidMain (String[]args) {3Object[] Objs = {"String type Test", 1,1.1};4         NewTest (). PrintArray (OBJS);5System.out.println ("********");6         NewTest (). PrintArray2 (OBJS);7System.out.println ("********");8         NewTest (). PrintArray2 ("I am string", 2,2.2);9System.out.println ("********");Ten         NewTest (). PrintArray2 (); One     } A      -      Public voidPrintArray (object[] objs) { -          for(Object obj:objs) { the System.out.println (obj.tostring ()); -         } -     } -      +      -      Public voidprintArray2 (Object ... objs) { +          for(Object obj:objs) { A System.out.println (obj.tostring ()); at         } -     } -}

The results are as follows:

Can see ... The meaning of this symbol is the variable parameter list
In the above printArray2, if the normal parameters are passed in, it automatically assembles the obtained parameters into an array and then automatically puts them into the objs so that they can be printed directly with the For-each loop.
And if the incoming itself is an array form, then the automatic conversion is no longer done.
And the last line is shown, indicating that the variable parameter list is possible even if nothing is passed in.

When using variable parameter types, you need to be aware of a small problem with overloading:

Before describing overloading, the way to differentiate overloads is that the parameter types are different, and this example can be considered an exception (or not an exception, because the mutable argument list is eventually converted into an array to handle)

Second, clean up

1) Finalize ()

The Finalize () method can be used to do some cleanup work, but it is not similar to a destructor in C + +, it is not written and will be executed immediately, this must be noted.
The design of this point is also a more intelligent design in Java, because to perform these cleanup operations itself, will waste your memory, so Java design is not necessary to write finalize () immediately call it (like you were bitten by mosquitoes, do not need to go to hospital)

About the Finalize () method is not necessarily the time to execute, it must be noted that you write a database connection statement, after the completion of the process to remember to close the flow, this closed operation to write in fanally, rather than write in Finalize ().

2) Another point of finalize ()

Java can be called c,c++, but the Java cleanup mechanism can not automatically destroy c,c++ generated memory consumption (such as Java can not operate registers, but c,c++ is possible), then the common is in Finalize () write the corresponding c,c++ destructor, To clean up the rubbish they produce.

Third, the creation and destruction 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.