Java Programming Ideas: 5th Chapter initialization and cleanup

Source: Internet
Author: User

With the development of the computer revolution, "unsafe" programming has become one of the main reasons for the high cost of programming.

Initialization and cleanup are just two issues that involve security.

5.1 Using constructors to ensure initialization

The constructor name is the same as the class name and there is no return value

5.2 Method Overloading

Overloading and method overloading of constructors

5.2.1 Methods for distinguishing overloads

The parameter list is different (the order is also overloaded, but not recommended)

5.2.2 involves basic types of overloads

void print (char c) {System.out.println ("The character is" +c);
void print (Byte b) {System.out.println ("The Byte is" +b);
void print (short s) {System.out.println ("the +s");}
void print (int i) {System.out.println ("the int is" +i);}
void print (long l) {System.out.println ("The Long is" +l);
void print (float f) {System.out.println ("The float is" +f);
void print (double d) {System.out.println ("The Double is" +d);

1) If you pass in a value directly, such as print (5), 5 is treated as int x = 5; Print (x) to find the most appropriate print (int)

2) A type other than a constant will find the most appropriate type, without the same type, but with a larger type will raise the type

3) Char is a special case, if there is no print (char) then char as int, execute print (int)

4) need to use cast to put in a small type of method

5.2.3 to differentiate overloaded methods with return values

void F () {}

int F () {return 1;}

This is not possible, because many times we do not need to return the value, just call it, then we will write:

f (); it's impossible to tell which way it is.

5.3 Default Constructors

If there are no constructors in the class, the compiler will help you get one.

I wouldn't have built one for you.

5.4 This keyword

Can be used to refer to the "current object" when invoking an object method to resolve the problem of parameter and domain name duplication.

Call constructor in 5.4.1 constructor

This (parameter);

Must be placed in the first row and can only be called one at most.

5.4.2 The meaning of static

The static method is the method without this, which belongs to the class and does not belong to the object.

5.5 Cleanup: Final processing and garbage collection

The Java garbage collector is responsible for reclaiming objects created by new and no longer being used.

The special case is that if an object obtains a chunk of memory in a non-new way, the GC does not know how to reclaim this particular memory. Java provides the finalize () method, which works "assuming" that once the garbage collector is ready to release the storage space occupied by the object, the Finalize () method is called first and the object memory is actually reclaimed when the next garbage collection action occurs. So the Finalize () method can do some important cleanup work during garbage collection.

Here doubts thank @launcher's careful answer.

---------------Solution Start

That is, you have such a class, pseudo code:

Class a{

IntPtr M_pbuffer;

A () {

M_pbuffer = malloc (1000);

}

void Finalize () {

Free (_m_pbuffer);

}
}

The A object does not use new, but uses malloc to obtain a special memory area of M_pbuffer.

A = new A (); Or use new to get the object A

However, object a itself does not have a special memory area of new M_pbuffer, which is not released by the GC, so you need to implement finalize and release it manually.

End of---------------Solution

Here's a myth:

Finalize () is the same as a destructor in C + +, which must be used when destroying an object.

This is not correct, the object in C + + must be destroyed, that is, the destructor must be called, and Java objects are not always garbage collection. Other words:

1) object may not be garbage collected

2) garbage collection does not equal destruction

5.5.1 Finalize () use

3) garbage collection is only related to memory.

Regardless of how the object is created in Java, the GC reclaims its memory and restricts the need for Finalize () to a special case: the object is allocated storage space in a way other than the object being created.

The reason to have Finalize () is that it is possible to use a C-like approach when allocating memory, which occurs primarily when calling local code using Java, such as allocating storage space with the malloc () function Series C, so you need to call the local code in Finalize (). Use Free () to release the memory.

So don't use Finalize () too much, it's not the right place to do regular cleanup work.

5.5.2, you have to clean up.

All objects in C + + will be destroyed. Includes local objects (created on the stack) and objects created with new (delete call destructor).

Java does not allow the creation of local objects, it must be created with new, and no delete is available, freeing object memory to be done only by the GC, but it is not guaranteed to be recycled. If you want to perform cleanup operations outside of the free storage space, you have to explicitly use it in Finalize (), which is equivalent to destructor, but not easy to refactor.

If the JVM is not running out of memory, it will not waste time and resources doing garbage collection to recover memory.

5.5.3 Termination conditions

Because the Finalize () method is executed before garbage collection, the replication Finalize method can detect whether the "logically" object should be terminated. Use this to find out if there is a defect in the program. For example, an object represents an open file, and we stipulate that the object must be turned off when it is reclaimed. In order to detect whether a program is defective, we can make a copy of the Finalize method to detect whether the state is open, and then see if there is a problem in a finalize, although finalize is not necessarily called, but it is critical that we know if there is a problem once the call is made.

We can use System.GC () to suggest GC operations, but the JVM may not listen to us.

How the 5.5.4 garbage collector works

Java Programming Ideas: 5th Chapter initialization and cleanup

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.