Java class / API interface

This chapter collection classes / interfaces API are:

Object class, enum, packaging class, interface Comparable, like Arrays, abnormal,

Object class

public String toString () : [the information of an object is represented by a string, as far as possible to be able straightforward, it is recommended that subclasses override]

public Class <?> getClass () : [ "running" Get object type]

void public Finalize (): [ When an object is determined as the garbage to be called by the garbage collector GC, finalize each object () method is invoked only once ]

int public hashCode () : returns a [] value object hashCode

General agreement:

(1) If an object involved in the calculation of the value of hashCode member variable is not modified, then the program is running, each acquired hashCode unchanged.

(2) If two objects hashCode different, then the two must not "equal"

(3) If the value hashCode two identical, then the two objects are not necessarily "equal"

(4) If the two are equal, then their hashCode values ​​must be the same.

public Boolean the equals (Object obj): [ for determining the current object to the specified object is equal}     

The default implementation, is equivalent to "==" comparison object memory address.

Subclasses can choose to rewrite, rewrite some of the requirements and principles: When overriding equals, be sure to override the hashCode method. Uniformity, symmetry, reflexivity, transitivity, the null and non-null target must be false equals

Object protected clone () throws CloneNotSupportedException: [Get clone for an object]    

All types can override this method, which is to obtain a clone object with the object, and the current object is to make a variety of attributes for identical objects. Of course certainly different address.

Calling this method must implement java.lang.Cloneable interfaces .

enumerate

1. toString () : returns the constant [default name (object name), the process may continue to manually override]
2.name (): returns the constant [name (object name)]
3. ORDINAL () : [ returns the constant sequence number, starting from 0] default
4. values () : returns all the constants [enumeration class objects of the return type is an array type of the current enumeration, is a static method]
5. the valueOf (String name ) : [Gets an enumeration object based enumeration constant object name]

Wrapper class

1, switching between the basic data types and string

. 1) STR = String String.valueOf (XXX) ; [Switch to the basic data types string ]

2) In addition to the Character class, all other packaging class has a static method parseXxx

  A = int Integer.parseIn T ( "integer string"); [String into corresponding primitive type ]

3) the string into the packaging , may then automatically unpack basic data type (type of return value is a wrapper class)

  A = int Integer.valueOf ( "integer string"); [string into packaging ]

Maximum and minimum 2, data type

Integer.MAX_VALUE and Integer.MIN_VALUE {return type corresponding to the maximum / small constant value]

3, character transfer case

Character.toUpperCase ( 'x');                                     [] uppercase characters turn
Character.toLowerCase ( 'X');   [] characters small letter

4, Integer to binary

Integer.toBinaryString (int i) {Integer to binary]
Integer.toHexString (int i) {Integer to Hex]
Integer.toOctalString (int i) {Integer octal]

Interface to the Comparable, Comparator

int compareTo (Object obj); [ Press attribute value of the object according to the sort NATURAL]

The first step: the object to which to compare the size of the class, which java.lang.Comparable class implements an interface, and override methods

Step Two: When you compare the size of the object, the object by calling the compareTo method, decide who is great who is small according to the return value of the method.

Interface states: call object returns a value greater than 0 is greater than the specified object, it is less than the specified object is less than 0, 0 is equal to two object values ​​are equal,

(Public static void sort java.util.Arrays array of tools (Object [] a) is so implemented)

int compare (Object o1, Object o2 );                             [ Press attribute value of the object according to the sort customized]

The first step: writing a class, we call comparator type, implement java.util.Comparator interface and override the method

Step Two: comparing the size of the object by comparing the type of call to compare () method, to compare the size of the object as its compare two methods of passing parameters, who determines who large small value in accordance with the method returns.

  • o1 o2 objects returned a positive integer greater than

  • o1 o2 negative integer less than the object

  • o1 o2 returned object is equal to zero

(Public static java.util.Arrays array of tools like <T> void sort (T [] a, Comparator <? Super T> c) this is done)

java.lang.Cloneable interfaces, and classes with the use of Object                [] for copying objects

Class Arrays

This class contains for manipulating arrays (such as sorting and searching a variety of methods). This class also contains an array as to allow the list to view the static factory .

Press according to parameter type may be in ascending order, also specified range ascending order.

Arrays.sort (strArray, String.CASE_INSENSITIVE_ORDER); [sort] Ignore case

Arrays.sort (strArray, Collections.reverseOrder ()); [reverse sequence]

Arrays.sort (strArray, String.CASE_INSENSITIVE_ORDER); [Ignore case reverse the sort order]

sort(T[] a, Comparator<? super T> c)   [Specified array of objects sorted according to the order of the specified comparator. (In conjunction with the above interfaces)]

abnormal

e.toString (): [get the kind of anomalies and errors]

e.getMessage (): get the error message []

Guess you like

Origin www.cnblogs.com/Open-ing/p/11891959.html