effective java second edition

Read about effective java second edition, The latest news, videos, and discussion topics about effective java second edition from alibabacloud.com

Effective Java-----About deferred initialization

the domain is initialized.private volatile fieldtype field; FieldType GetField () { FieldType result = field; if (result = = null) { //first check (no locking) synchronized (this) { result = field; if (result = = null) {//second check (with locking) field = result = Computefieldvalue ();}} } return result;}Two check the domain value, the first check is not locked, see if the domain is initialized, the second check is locked, only when the

"Effective Java" 5, overwrite equals when always overwrite Hashcode

; result = * result + linenumber; return result; } */ //if an object is not constantly changing, and the cost is relatively large, you have to consider it. Hash code is cached inside the object//with volatile modifiers, the thread will read the variable's most-modified value each time it uses the variable. Private volatile inthashcode; @Override Public inthashcode () {intresult =hashcode; if(Result = = 0) {result= 17; Result= * result +AreaCode; Result= * result +prefix;

Effective Java Reading notes--Methods

considered directly as an object T, then int[] as a whole is treated as an argument, not as an int parameter In addition, when it comes to performance, the array allocation and initialization of each invocation of a mutable parameter may be difficult to meet performance requirements, consider the following: If a call to a method 95% has three or fewer arguments, it declares 5 overloads of the method:1 Public voidFun () {}2 Public voidFunintA1) {}3 Public voidFunintA1,intA2) {}4 Public

[Effective Java Reading notes] Chapter III general methods for all objects eighth----? Reviews

, the number of times the operation gets the same value5. Non-nullability. It is generally not necessary to check null = = obj shown in equals, because if (obj instanceof Integer) {} has done this workA few suggestions for writing equals:1. First Use = = check whether a reference to this object2. Use instanceof to check if the parameter is of the correct type3.equals cover Hashcode (), see Nineth4. Don't let equals be too smart and too complicated5. Do not tell the equals argument to another ty

[Effective Java Reading notes] 6th Zhang Yi Lifting and annotations

30th use enum instead of intIn all, there are several advantages to using enum1. Compile-time type safety,2. Can be guaranteed to be the value of their own definition, there is no monthly risk,3. Each enum type has its own namespace4. Enumeration can add arbitrary methods and fields5. The enumeration type is final and cannot be instantiated, that is, the instance is controllable.31st Replace ordinal with instance fieldThat is, do not rely on enumerations to specify their values in order to defin

"Effective Java" 8, preferred type-safe heterogeneous containers

the data is stored in the type of object, then you need to do a type conversion returntype.cast (Data.get (type)); } Public Static voidMain (string[] args) {Manytypeclass MTC=NewManytypeclass (); Mtc.putdata (String.class, "Cutter_point"); Mtc.putdata (Integer.class, 0XCAB145DE); Mtc.putdata (Class.class, Manytypeclass.class); Mtc.putdata (Manytypeclass.class, MTC); String datastring= Mtc.getdate (String.class); intDatainteger = Mtc.getdate (Integer.class); Classclass); Manytypec

"Effective Java" 9, using Enummap instead of ordinal index

the corresponding set set for(inti = 0; i i) {Herbsbytype[i]=NewHashset(); } //determines the position of the corresponding type collection based on the location of the enumeration type and adds the data to it for(Herbold ho:garden) {//ordinal, this returns the position of the enumeration type at the time of declarationHerbsbytype[ho.gettype (). Ordinal ()].add (HO); } //Traverse for(inti = 0; i i) {//This is a running operation here. /*** But he

Effective Java Reading notes six methods

First, check the validity of the parameters1, consider the limitations of the parameters, write the restrictions to the document, at the beginning of the method by explicit check to enforce these restrictions.Second, if necessary, a protective copy1. If a class has a mutable component that is obtained or returned from the client, the class must consider a protective copy of these components.2, if the copy cost is high, the class can also trust his client, do not copy protection, but to document

Effective three classes and interfaces of Java reading notes

type frame of non-hierarchical structure4, the interface can safely enhance the function of the class.5, by providing an abstract skeleton implementation for the important interface, the advantages of the interface and abstract class together.Abstract classes that define types that allow multiple implementations have a distinct advantage over interfaces: The evolution of an abstract class is much easier than the evolution of an interface.VII. interfaces for defining types only1, the constant in

The 11th chapter of effective Java serialization

persistence, it is necessary for this class to implement the Serializable interface. Based on experience, value classes such as date and BigInteger should implement serializable, and most of the collection classes should. A class that represents an activity entity, such as a thread pool, should not implement serializable generally.Classes designed for inheritance should implement the serializable interface as little as possible, and the user's interface should inherit as little as possible from

Effective Java-Carefully overwrite clone

(); }}Although the elements was taken out of the clone once, but the premise of this approach is that elements is not final.It's normal. However, clone cannot be compatible with immutable field referencing mutable objects.If an element of an array is a reference type, the problem still occurs when an element changes.Here, for example Hashtable, the elements in Hashtable are entry with their inner classes.private static class EntryIf you clone the elements directly as in the stack example, the

Effective Java-note overwrite equals

unless a party is modified.Rather than the Equals method, this class should be designed to be variable or immutable when thinking about writing a class.If it is immutable, consistency needs to be ensured.With these provisions in mind, here are some suggestions for overriding equals: The first step uses the "= =" operation to verify that the reference is the same, so that unnecessary comparison operations are done. Use instanceof to check the type of the parameter. Check all cri

Effective Java experience, generics

. arrays and generics have this very different type rule, arrays are synergistic, generics are immutable and can be erased. Therefore, the array provides runtime type safety, but no compile-type security. Generally speaking, there is no good mix between the 2, preferably using a list instead of an array.4. preference is given to generics. generally it takes 2 steps for a class to be generic, declares a generic, and changes all objects to the generic parameter E. Finally, ensure that the conversi

[Effective Java] creating and destroying objects

parameter, so the static factory method is actually a type conversion method. Of A more concise alternative to valueof, used in Enumset and popular. GetInstance The returned instance is described by the parameters of the method, but cannot be said to have the same value as the parameter. Newinstance Like getinstance, but newinstance can ensure that every instance returned is different from all other instances GetType Like getinstance, but

Effective Java Item2:consider A builder when faced with many constructor parameters

Item2:consider a builder when faced with many constructor parametersWhen a constructor method has more than one parameter, you can consider using the builder approach for processing.Instance code: Public classnutritionfacts {Private Final intservingsize; Private Final intservings; Private Final intcalories; Private Final intfat; Private Final intsodium; Private Final intcarbohydrate; Public Static classbuilder{//The following two parameters are required Private Final intservingsize; Priv

Returns a 0-length array or collection instead of null--effective Java reading notes

/** * Returns a 0-length array or collection instead of NULL * * @author Liu Xiangfeng * */ Public class Shop { PrivateList//0 Length array constants are passed to the ToArray method to indicate the expected return type Private Final Staticobject[] Empty_object_array =Newobject[0]; Public Shop(listSuper(); This. objectList = objectList; }/** * * @return An array containing all of the objects in the shop */ PublicObject[]getobjects() {//If the collection is empty, he will use a 0-len

"Effective Java reading notes" Creating and destroying Objects (i): Consider using static factory methods instead of constructors

Class can provide a static method that returns a static instance of a class, such as a static method that obtains an instance of a Boolean wrapper class1 Public Static Boolean ValueOf (boolean b) {2 return (b? True:false); 3 }Advantages:1, there is a name. When a class requires more than one constructor with the same signature, the static factory method is used instead of the constructor, and the name is carefully chosen to highlight the difference between them. such as: biginteger.

"Effective Java" classes and objects

{Interfacea a=NewInterfacea () {//member Anonymous Class}; Public Static voidMain (string[] args) {Interfacea a=NewInterfacea () {//local Anonymous class};//The above two are implemented by implementing an interface anonymous class, called an interface anonymous class, can also be inherited by the classTest test =NewTest () {};//inherited anonymous Classes//can also be on the parameterNewThread (NewRunnable () {@Override Public voidrun () {}}). Start ();//belonging to a local anonymous class}Pr

Java Theory and practice--effective and correct definition of hashcode () and Equals () __java

Each Java object has a hashcode () and Equals () method. Many classes ignore (Override) default implementations of these methods to provide deeper semantic comparability between object instances. In the Java Philosophy and Practice section, Java Developer Brian Goetz introduces you to the rules and guidelines you should follow when creating

Effective Java Reading

think in Java , effective Java These two books have always been in the Java ecosystem for enduring. Originally wanted to turn over think in Java this mountain, but read half to give up. Too long, so unbearable, and then big interest, also by too much cumbersome text erased

Total Pages: 15 1 .... 5 6 7 8 9 .... 15 Go to: Go

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.