Android Application Development and Improvement series (1) -- Reading Notes of Practical Java Chinese edition (I)

Source: Internet
Author: User

Books
Translated by Hou Jie and Liu yongdan of Practical Java Chinese edition 03
This book, coupled with objective Java, provides a detailed, profound, and practical introduction, analysis, and examples of the aforementioned important and basic technologies, and presents them in the form of independent clauses, the performance in the content is very good, readability and ease of reading.
This book focuses on the actual problems encountered in Java programming. It can be said that the topics listed in the book are the FAQ that puzzles many Java programmers. The author is good at using appropriate examples to explain the problem and give pertinent suggestions in the usual language. These suggestions can be directly integrated into the programming work in a single language. It can be seen that they are indeed from the insights gained by the author from practice.
 
Body
Note: entries and terms may differ from those in books, but try to keep them as they are and add your own understanding.
I. general technology
1. The only parameter transfer mechanism in Java: by value ).
Note: When a parameter is an object, the reference of the object is passed as a value. (My understanding: transferring pointer copies)
 
2. Use final for unchanged data and object references
Note: when an object is declared as final, it cannot be changed, but its value can be changed.
 
3. By default, all non-private and non-static functions can be overwritten.
3.1 If the function does not want to be overwritten by the quilt class, declare the function as final.
3.2 If the class does not want to be overwritten by the quilt class, declare the class as final, and prohibit overwriting all functions of the class. It also improves performance.
 
4. Carefully select between arrays and Vectors.
Array Vector
Basic data type and reference type
The default value of the element is Yes No.
Auto-changing, fixed, and dynamic growth
Fast Yes No
Note: The Vector is actually implemented in arrays.
 
5. polymorphism is better than instanceof, and instanceof is used only when necessary.
Note: For example, to convert a parent class to a derived class, you must use instanceof.
 
6. If you do not need an object reference, set it to null.
Note: If you still need to execute large code before using the reference function of a local variable, you can set it to null after use, so that it may be recycled at the next running of the garbage collector.
 
Ii. Object and equality
1. Difference = and equals ()
Note: Use = to test whether the two basic types are identical, or whether the two object references point to the same object. Use equals () to compare whether the content of the two objects is equal.
 
2. Do not rely on the default Implementation of equals ().
Note: All Java objects inherit from java. lang. Object. The default equals () only checks whether the Object reference points to the same Object.
 
3. equals () implementation suggestions:
3.1 check whether it is equal to this
Note: test whether to point to the same object
3.2 getClass () is preferred ()
Note: getClass () returns the runtime class of an object ). Make sure that only the objects generated by the same class have the opportunity to be considered equal. Example:
 
Public class Base {

@ Override
Public boolean equals (Object obj ){
If (obj! = Null & getClass () = obj. getClass ()){
// Continue to compare equality or directly return true
}
Return false;
}
}
 
3.3 call super. equals () to invoke relevant behavior of the parent class
Note: When you write equals () for a derived class, you must check all the parent classes except java. lang. Object To see if they all implement equals (). If so, you must call super. equals ().
3.4 exercise caution when using instanceof in the equals () function
Note: If the comparison between a derived class and its parent class is allowed, instanceof (getClass () may always return false ). Note that instanceof is similar to the is-a syntax. Pay attention to the position (subclass instanceof parent class-> returns true; otherwise, false ).
 
Iii. Exception Handling
1. Identify abnormal process mechanism
Note: try section-> [catch capture section] [Optional]-> finally end section.
 
2. Handling exceptions
If an exception is generated but not captured, the thread where the exception occurs will be interrupted. Handling exception:
A) capture and process it to prevent further propagation.
B) capture and throw a new exception to the caller.
Note: ensure that the newly thrown exception contains information related to the original exception, so as not to lose important information.
C). If this parameter is not captured, send it to the caller.
Output Error message:
A). exceptions that have occurred in log file records
B). Output exception
Output to standard error stream: e. printStackTrace ().
 
3. Avoid overwrite exceptions
NOTE: If an uncaptured exception is thrown in the catch or finally segment, the new exception overwrites the exception thrown by try. Only one exception can be propagated to the call segment.
 
4. throws clause
Note: the intention of the throws clause is to notify the function caller of possible exceptions. The compiler forces the caller to capture these listed exceptions, so do not add them until the end of the development cycle.
If the overwriting object (a parent class function) does not throw any exception, the overwriting function will cause an exception due to the addition of code, so you must capture and handle the exception in the new function.
 
5. Use finally to avoid resource leakage
Note: The finally code segment is executed no matter whether an exception occurs or not. It is especially suitable for maintaining the internal status of the object and clearing non-memory resources.
 
6. Suggestions:
A) do not run the return, break, or continue statement from the try partition.
NOTE: If both the try and finally segments return data, the finally segment return data will be returned.
B) Place the try/catch section outside the loop.
Note: exceptions have a negative impact on code performance.
C) do not use exceptions for process control.
D). An exception is thrown in the constructor.
E). Restore the object to a valid state before throwing an exception.
Note: consider what will happen next time you execute this Code and whether the code can still run normally.
 
End
It is expected that the first several articles in this series will be the Reading Notes of Practical Java and objective Java. The subsequent content is still being planned, you are also welcome to give me the content you are interested in as a potential series of articles.

 

From farmer's uncle

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.