JavaGuide (learning note 2)

xiaoxiao2021-03-06  84

20: String connection StringBuffer, reduce the generated object, helps improve efficiency.

Public string attachstr (String [] strdim) {

StringBuffer RTNSTRBUFF = New stringbuffer ();

For (int i = 0; i

// StringBuffer よ よ よ よ よ

RtnstrBuff.Append (strdim [i]);

}

Return RTNSTRBUFF.TOSTRING ();

}

21: Try to write "XXX" .Equals (string) instead of string.equals ("xxx").

The latter should judge whether the string is null, the former is more concise.

22: Introduction to the Exception class during custom exceptions, not Error or Throwable. Because Error classes are often used for severe hardware errors within the system. And in most cases, do not use custom exception classes as runtime example RuntimeException subclasses. In addition, class names of custom exceptions are often ended at Exception.

Public Class SampleaaaaAexception

Extends Exception {← OK

Public Class Samplebbbexception

Extends ioException {← ng

Public Class Samplecccexception

Extends runtimeException {← ng

23: Do not declare Exception, RuntimeException in the catch statement.

Public class badsample {

Public void badsamplethod () {

Try {

System.in.read ();

} catch (exception e) {// violates

E.PrintStackTrace ();

}

}

}

Public class fixedsample {

Public void fixedsampleMethod () {

Try {

System.in.read ();

} catch (ioException e) {// correction 済み

E.PrintStackTrace ();

}

}

}

Similarly, don't directly throw those upper Exception, you should specific Exception

24: For a plurality of TRY, multiple catchments are corresponding to him, and the order of the CATCH block should be special.

25: Do not do exception processing in the loop, do not have time Catch blocks, Catch is not Return in the fast, so throwing out the abnormality, do not cut in the meta-way.

26: It is recommended to use ArrayList, Hashmap, Iterator, but arraylist, hashmap is not synchronized, so in multiple threads, it is not safe. At this time, you can use Java.util.Collections SynchronizedListions SynchronizedListion and SynchronizedMap methods.

List list = collections.synchronized (new arraylist ());

...

SYNCHRONIZED (List) {

Iterator i = list.iterator (); //must be in synchronized block

While (I.hasNext ())

Foo (i.next ());

27: If it is involved in stack, queue, etc., you should consider using List, for quick plug, delete elements, you should use LinkedList, if you need to quickly randomly access elements, you should use ArrayList. If the program is in a single threaded environment, or the access is only in one thread, considering the non-synchronous class, its efficiency is high, if multiple threads may operate a class at the same time, the synchronous class should be used. Pay special attention to the operation of the hash table, as a key to Key, to properly reply Equals and HashCode methods. Try to return to the interface instead of the actual type, if you returns List instead of arraylist, the client code does not have to change when you need to change ArrayList to linkedList. This is for abstract programming.

28: If you want to do more accurate precision, you can use BigDecimal. During basic numerical types of type conversion, do not convert high-precision to low precision and prevent accuracy loss.

PUBLIC CASTTEST {

Public static void main (String [] args) {

Double Doubletype = 2.75;

INT INTTTYPE = (int) DoubleType; // Double を INT キャ キャ ト

System.out.println ("Double:" DoubleType);

System.out.println ("INT: INTTYPE);

}

}

実行 结果 は は:::::

Double: 2.75

Int: 2

When the real is compared, due to the problem,

Example 1:

IF (ABS (A-B) <1.0e-6) {

. . .

}

Example 2:

IF (a-b> -1.0e-6) {

. . .

}

Instead

Example 1:

IF (a == b) {

. . .

}

Example 2:

IF (a> b) {

. . .

}

29: Do not turn off the stream in TRY, do not turn off the flow in TRY, which will be closed in Final.

30: A good JDBC's Sample.

PUBLIC CLASS SAMPLE {

// ...

Public void searchsomething (...) {

...

Connection conn = NULL;

PreparedStatement Stmt = NULL;

ResultSet RS = NULL;

Try {

CONN = ConnectionManager.getConnection ();

STMT = conn.preparestatement (SQL);

RS = stmt.executeQuery ();

...

} catch (...) {

...

} finally {

IF (rs! = null) {

Try {

Rs.close ();

} catch (exception e) {

... // Log ど

}

}

IF (stmt! = null) {

Try {

Stmt.close ();

} catch (exception e) {... // log ど ど

}

}

IF (conn! = null) {

Try {

CONN.CLOSE ();

} catch (exception e) {

... // Log ど

}

}

}

...

}

} (Reix after the future) is expected to increase together!

转载请注明原文地址:https://www.9cbs.com/read-107427.html

New Post(0)