Java programmer's path to growth (technical points you must know)

Source: Internet
Author: User

Original address http://iandroiddev.com/post/2012-03-13/16167710

Http://www.cnblogs.com/over140/archive/2012/01/29/2330758.html

1. constructor must not call methods that can be overwritten, either directly or indirectly.

 

2. interfaces should only be used to define types. They should not be used to export constants. (Note: do not define constants in the interface)

 

3. A safe and conservative policy is to never export two overload methods with the same number of parameters.

 

4. returns an array of zero length instead of null.

 

5. Nested classes

A nested class is a class defined inside another class. Its purpose is to provide services for its peripheral classes. There are four types of nesting:

5.1 Static member class)

The simplest nested class is better to regard it as a common class. It can access all the members of the peripheral class, including those declared as private. Like other static members, they also comply with the same accessible rules.

It is usually used as a public auxiliary class and is meaningful only when used together with its external class.

A private static member class is usually used to represent components of peripheral class objects. For example, a map instance usually has an entry object corresponding to every pair of key-value pairs in map. Although each entry is associated with a map, however, the methods on the entry (getkey, getvalue, and setvalue) do not need to access the map. Therefore, it is a waste to use non-static members to indicate the entry, and private static member classes are the best choice.

 

5.2 non-static member class (nonstatic member class)

Each instance of a non-static member class contains a reference pointing to an external class object. It takes time and space to maintain this reference.

It is usually used to define an adapter, which allows an instance of the peripheral class to be seen as an instance of another irrelevant class. For example, map interfaces often use non-static member classes to implement their set views.

 

5.3 Anonymous class)

No name. It is not a member of the peripheral class and is declared and instantiated at the same time. It can appear in any part of the code that allows the expression to appear. Generally, only methods in its interfaces or superclasses are implemented, and no new methods are declared. They should be very short.

Usage 1 is to create a function object, such as a comparator instance. For example:

Arrays. Sort (ARGs, new comparator <string> (){
@ Override
Public int compare (string obj1, string obj2 ){
Return obj1.length ()-obj2.length ();
}
});

Use 2 to create a process object, such as a thread, runable, or timetask instance.

Usage 3 is inside a static factory method, for example:

Static list intarraylist (finalint [] ){
Return new effecactlist <integer> (){

@ Override
Public integer get (INT location ){
Return a [location];
}

@ Override
Public int size (){
Return A. length;
}};
}

Usage 4: In a complex type security Enumeration type, it is used in the public static final domain initializer, for example:

Public abstract class operation {
Private final string name;

Operation (string name ){
This. Name = Name;
}

Public String tostring (){
Returnthis. Name;
}

Abstract double eval (Double X, Double Y );

Public static final operation plus = new operation ("+ "){
@ Override
Double eval (Double X, Double Y ){
Return X + Y;
}
};
}

 

5.4 local class)

With minimal use, you can declare local classes in any place where "local variables can be declared" and follow the same scope rules. Like Anonymous classes, they must be very short.

In short, if a nested class needs to be visible outside a single method, or it is too long to be placed inside a method, you should use a member class. If each instance of the member class needs a reference pointing to its peripheral instance, the member class is made non-static; otherwise, it is made static. Suppose a nested class is inside a method. If you only need to create its instance in one place and a pre-stored type already exists, you can describe the features of this class, then it is made into an anonymous class; otherwise it is made into a local class.

 

6. understand and use the library

Be familiar with Java. Lang, java. util, and Java. Io.

6.1 random. nextint (INT) generates a random integer.

6.2 sort by vector composed of collections. Sort (v) strings

6.3 collections. Sort (v, String. case_insensitive_order)

6.4 system. Out. println (arrays. aslist (A) print all elements in an array cyclically

6.5 obtain all the keys whose hashtable contains the same ing key value:

Map TMP = new hashmap (H1 );
TMP. entryset (). retainall (h2.entryset ());
Set result = TMP. keyset ();

6.6 arrays. tostring (a) print each element of the array

6.7 arrays. Equals (a1, a2) compares the two arrays and whether each element is equal.

 

7. Usage exception

7.1 checked exception)

By throwing a checked exception, the caller is forced to handle the exception in a catch clause or spread it out.

7.2 run-time exception)

Most runtime exceptions mean that API customers do not comply with the conventions established by API specifications. For example, the array is out of bounds.

All in all, the exception to be checked is used for recoverable conditions, and the runtime exception is used for program errors.

 

8. Try to use standard exceptions

8.1 The parameter passed by the illegalargumentexception caller is not suitable

8.2 nullpointexception NULL pointer exception

8.3 indexoutofboundsexception subscript out of bounds

8.4 concurrentmodificationexception concurrent modification is detected on the object without concurrent modification.

8.5 The unsupportedoperationexception object does not support the requested Method

Http://www.cnblogs.com/hgndinfo/

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.