Initialization and cleanup

Source: Internet
Author: User

Initialization and cleanup

To be honest, after reading the "initialization and cleanup" section in "JAVA programming ideology" carefully, I feel that nothing can be done, which makes me very disappointed and should be recorded in this blog, it is because we hope that we can thoroughly understand the "initialization" and "cleaning" information in the next day.

1. Identify overload methods with return values

I once did this silly thing. In projects, we often write our own DateUtils package to convert various date formats.

int getFromDate(Date date){}long getFromDate(Date date){}

For example, the above Code is intended to get the long timestamp (13 BITs) or int timestamp (10 bits) of the date by using the method name getFromDate ), I used to think that this writing method should be reasonable for the compiler, but the compiler fails.
You need to change it to the following statement:

int getIntFromDate(Date date){}long getLongFromDate(Date date){}

Sometimes you don't care about the return value of a method. You want other effects of a method call.

In this case, the compiler does not know which one you want to call for the getFromDate method?

 

Therefore, the return value differentiation overload method is not feasible.

II. this and static

I am very interested in this section. For these two keywords, Wang Er has never been "coming in depth" and should be summarized later.

Iii. Garbage Collection

Bruce spent a lot of text to introduce java's garbage collection mechanism. I didn't understand it, whether it was "mark-sweep" or "stop-Copy, sorry, I hate myself for being depressed.

Iv. Initialization

I remember watching videos from soldiers of Shang xuexiang, or from Teacher Li Xinghua of Magic Music technology, I spent some time introducing the sequence of java classes during initialization. Today I repeat it again, but I still don't feel like it.

① Variable Parameter List

Variable Parameter lists are useful, especially when assigning values to variables in SQL statements.

public void fillStatement(PreparedStatement pst, Object... paras) throws SQLException {    for (int i=0; i
  

When preprocessing SQL statements, we basically use "?" To replace the parameter, and then place the parameter in the Variable Parameter paras, which is very useful.

② Enumeration type

Wang Er thought that enumeration did not play its intended role. At least it seems that enumeration is still a little exhausting in the use process, but it is very important to use enumeration, that is the Singleton.

public enum Single {INSTANCE;}
It is very concise. Serial number mechanism is provided free of charge to prevent repeated instantiation.

These three points have been strongly recommended in objective java, the best way to implement singleston! There are many other versions of singleston. For details, refer to the Singleton design mode for a single instance.

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.