effective java ebook

Alibabacloud.com offers a wide variety of articles about effective java ebook, easily find your effective java ebook information here online.

Effective Java 3

an alternative to choose the following methods:Private Static Final thing[] Private_values = {...}; Public Static Final ListPrivate Static Final thing[] Private_values = {...}; Public Static Final thing[] VALUES () { return private_values.clone ();}9. In Java, public is not the highest access level, and if it is not in the export package, it will be private to the module.10. The public class should not have a public domain unless it is a public

Effective Java within class

Effective Java in the requirements of the class, interface, etc., summed up is enough to do, do not give too much access, the definition of the class is also, if a class is only used inside the class, then the class is defined as an inner class bar.The inner class is divided into four types:1, static inner class: Static inner class is in front of class more static keyword inner class, this class and class s

"Reading notes-effective Java" 02. Consider using the builder when you encounter multiple constructor parameters

construction Process There is a cost to creating the builder //Example Builder mode Public classnutritionfacts {Private Final intServingsize;//Required Parameters Private Final intservings;//Required Parameters Private Final intFat//Optional Parameters Private Final intSodium//Optional Parameters Public Static classBuilder {Private Final intservingsize; Private Final intservings; Private intFat = 0; Private intSodium = 0; PublicBuilder (intServingsize,intservin

Reading notes of "effective Java" Third, strengthen the Singleton property with a private constructor or enumeration type

is asked to create a second instance. In addition to the factory method there is a public domain method:public class Elvis2 {public static final Elvis2 INSTANCE = new Elvis2 ();p rivate Elvis2 () {}public void leavethebuilding () {} }The advantages of this approach are clear and unambiguous. The disadvantage is rigid, performance is not. The advantage of factory method is flexibility. The disadvantage is that it can be easily modified, such as changing to a unique instance of each thread that i

Java setting session timeout (fail) Three ways to set the session's effective time

1. Set in the Web container (for example, Tomcat here)Set in Tomcat-5.0.28\conf\web.xml, the following is the default configuration in Tomcat 5.0:[HTML] View plain copy - session-config> session-timeout>30session-timeout> session-config> Tomcat default session timeout is 30 minutes, can be modified as needed, negative or 0 is not limit session failure time.2. Set up in the project's Web. xml[HTML] View plain copy 3. Using

Hardening the Singleton property with a private constructor or enumeration type--effective Java reading notes

the face of complex serialization or reflection attacks. * Single-element enumeration types have become the best way to implement Singleton. * @author Liu Xiangfeng * * * * Public enumEnumsingleton {INSTANCE; Public void leavethebuilding(){}}/** * Serialization of Singleton mode * @author Liu Xiangfeng * */ Public class Serializesingleton implements Serializable { //Declare all instance domains to be instantaneous transient Private Static FinalSerializesingleton INSTANCE =NewSerializesin

"Effective Java"--exception

keep the failure atomicA failed method call should keep the object in the state it was before it was called, and there are several ways to do so Using Immutable objects Check parameter validity before changing state, throw exception early Put the potentially failed action before the change state, so that the failure can end prematurely without leaving the object in an inconsistent state Write recovery code (infrequently used), typically for data that has persisted To ma

Effective Java-Creating and destroying objects

1, consider using the static factory method instead of the constructor.Here are some advantages: The static method factory method has a variable name. For example, a true object defined in a Boolean class with a name that is very intuitive. Boolean.truepublic static Final Boolean true = new Boolean (true); You do not have to create new objects every time you call. If creating objects is expensive, using static classes can improve performance. Any subcla

Effective Java English Second Edition reading note Item 2:consider a builder when faced with many constructor parameters.

; } PublicBuilder Calories (intcalories) { This. Calories =calories; return This; } PublicBuilder Fat (intfat) { This. Fat =fat; return This; } PublicBuilder Sodium (intsodium) { This. sodium =sodium; return This; } PublicBuilder Carbohydrate (intcarbohydrate) { This. Carbohydrate =carbohydrate; return This; } PublicNutritionfactsbuilder Build () {return NewNutritionfactsbuilder ( This); } } Private

[Effective Java Reading notes] Chapter II Creating and destroying objects the first article

Chapter II Creating and Destroying objectsThe first one uses the static factory method instead of the constructor, because: Static factory methods can have different names, that is, the constructor can only be distinguished by different parameters, and static factories can express different purposes in their names. The static factory method creates a new object without each invocation (in fact because it is static, so it can only be used as static, so it is created early and does not need

"Effective Java" 7, preference for generic methods

.item27; Public classgenericsingletonfactory{//Create object objects first, temporarily override T objects Private StaticUnaryfunctionNewUnaryfunction() { Publicobject Apply (object arg) {returnArg; } }; //according to T, the identity_function is converted to the corresponding type, because Identity_function returns the object type parameter unmodified, so the following type conversion is safe@SuppressWarnings ("Unchecked") Public Staticidentityfunction () {return(una

"Effective Java" 12, avoid over-synchronization

= = 23) { //Thread pool, which creates a thread pool of a single threads, creates a new thread instead of executing the task if the current thread breaks abruptly when the task is executedExecutorservice executor =Executors.newsinglethreadexecutor (); Finalsetobserver This; Try{executor.submit (NewRunnable () {@Override Public voidrun () {//There's going to be a deadlock.Set.removeobserver (Observer); }}). get (); } Catch(i

Effective Java Learning--16th: compound takes precedence over inheritance

(collectionreturnS.retainall (c); } @Override PublicBooleanRemoveAll(collectionreturnS.removeall (c); } @Override Public void Clear() {s.clear (); }}Myset2.javaPackage Cczu.edu.test2;import java.util.Collection; Public class MySet2e> extends forwardingsete>{ Private intAddcount =0; @Override Public BooleanAdd (e e) {addcount++;return Super. Add (e); } @Override Public BooleanAddAll (collectionreturn Super. AddAll (c); } Public intGetaddcount () {returnAddcount; }}Test@Test Public voidTest2

[Reading notes] Effective Java Chapter III

the general conventions of Object.hashcode, which prevents the class from working properly with all hash-based collections, such as Hashmap,hashset and Hashtable.always overwrite ToStringThis item is mostly intended to provide the necessary critical information when printing data, because the default ToString method only returns the unsigned hexadecimal representation of the class name [email protected]+ hash code.cover clone with careThe purpose of the Cloneable interface is expressed as an ob

"Effective Java" reading notes (a): Replace constructors with multiple parameters with builder mode

; } Public Static voidMain (string[] args) {Builderdemo demo=NewBuilder (calories). Carbohydrate (43). build (); }}Such code is easy to write and read. Furthermore, constraints can be imposed on their parameters, and the build method can examine these constraints, copy the parameters from the builder to the object, and test them in the object domain.Insufficient builder mode: 1. You need to create the builder before creating the object, which is not applicable in a very performance-oriented

[Effective Java Reading notes] Chapter III Class and interface 16th

The 16th compound takes precedence over inheritanceIf you are unsure of the relationship between B and a, and B is indeed a, then you should not use B to inherit a, or you will expose the implementation details, and your implementation will be limited to the original implementation, Is-a.The first example of the book cited implements a class extends HashSet class, because the lack of understanding of the AddAll method of the HashSet class (AddAll calls the Add method repeatedly), resulting in a

"Effective Java" service provider framework

The static factory method returns the class to which the object belongs, and can not be present when writing a class that contains the static factory method. This flexible, static factory approach forms the basis of the service provider Framework (Provider framework), such as the JDBC API. The service provider framework refers to a system in which multiple service providers implement a service (interface) that provides multiple implementations of the service provider's clients and decouples them

Effective Java-minimizing variability

, there are two ways we can solve this problem: Use the basic type as much as possible within the class. If you can pre-think that those states will often change, then provide a package-level private matching class, for example, StringBuilder is relative to string. Also, if, for some reason, you cannot use the final modifier immutable class, but you do not want to be extended, there are other methods in addition to using final.The method is to replace all public constructors wi

"Effective Java" reading notes (v) using dependency injection instead of the original resource dependency

:// Dependency Injection provides flexibility and testability Public class spellchecker { private final Lexicon dictionary; Public spellchecker (Lexicon dictionary) { this. Dictionary= Objects.requirenonnull ( dictionary); } Public boolean isValid (String word) {...} Public List Suggestions (String typo) {...}}So the customer needs to use what dictionary is the customer's own decision, and our code does not need to maintain multiple copies.The popular explanation f

Effective Java experience, common approach

field that invokes its Hashcode method on each of these elements.[3] The above computed hash code is saved to the INT variable C, and then executed Result=37*result+c;[4] returns result.3. Always overwrite the ToString method. only override this method, other such as arrays.tostring (), and the toString of the collection class. When you call the output of the class, you invoke the ToString that the class has already implemented.4. Call the Clone method with caution. When you are designing a cla

Total Pages: 8 1 .... 4 5 6 7 8 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.