more effective java

Discover more effective java, include the articles, news, trends, analysis and practical advice about more effective java on alibabacloud.com

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 common methods for all objects in Java learning notes

domain: Recursively calling the Equals methodFloat field: Float.compare methodDouble field: Double.compare methodE, after completing the Equeal method, ask yourself three questions: Is it symmetrical, transitive, and consistent?3. Some cautions1, after covering the Equals method, be sure to overwrite the Hashcode method2. Do not attempt to make the Equals method too intelligent3. Do not replace object objects in the equals declaration with other types4. Note Using @override annotations to ensur

Effective Java chapter II objects (1)

using the builderPackage theone;public class Twoobject {private int A;private int B;private int C;public static class Bulider {private int A;private int B;private int C;Public Bulider A (int a) {THIS.A = A;return this;}Public Bulider B (int b) {this.b = b;return this;}Public Bulider C (int c) {THIS.C = C;return this;}Public Twoobject Bulid () {return new Twoobject (this);}}Public Twoobject (Bulider b) {THIS.A=B.A;this.b=b.b;THIS.C=B.C;}public static void Main (string[] args) {Twoobject to=new T

Effective Java-Preface

A few days ago Amazon accidentally recommended this book, online search for the next introduction, found to be very interested in the form.Instead of flipping through the basics, the author uses the best practices to present them.And did not spend valuable space on the programming of graphical interfaces. Then decisive order.-----After reading the introduction of the first chapter, as expected, some places are not fluent in translation, but do not affect understanding. The book is divided into 1

Effective Java--4 generics

[] strings= {"F", "Z", "K"}; Unaryfunctionidentityfunction (); for(String string:strings) System.out.println (samestring.apply (string)); Number[] Numbers= {1, 2.0, 3L}; Unaryfunctionidentityfunction (); for(number Number:numbers) System.out.println (samenumber.apply (number)); }}28th: The flexibility of using the Fraiti API with restricted wildcard Public classtwentyeighth { Public Staticextendse> s1, setextendsE>S2) {SetNewHashset(S1); Set.addall (S2); returnset; } Public Static voidMai

Effective Java tenth concurrency avoid excessive synchronization of reading notes

Avoid over-synchronizationIn order to avoid active failure and security failures, a synchronized method or code is fast, never give up control of the client.Because it's alien, it's not controlled.Move the foreign method call out of the synchronized code fast.Create a SnapshotUse concurrent collections, copyonwritearaylist. A Variant that belongs to ArrayList, where all writes are implemented by copying the entire underlying array. Because the internal data is used for no change, the iterations

Effective java-Create Dafabet lottery ticket platform for lease and destroy objects

parameterless builder method to generate the immutable object. This method is more flexible and easy to read, and can increase the constraint condition for the parameter. It also has its own shortcomings, such as in some very performance-oriented circumstances, its overhead may be problematic. This pattern is more verbose than an overlapping constructor, so it is used only when a number of parameters are available, especially when most parameters are optional.Third #, hardening the Singleton pr

Three tools for easy and effective inspection of Java code: PMD, Checkstyle, and Jalopy

projects agreed by the company code. If improving code quality is the primary goal, then PMD is a good choice. But if you want more features and really use tools to modify the code, you should try jalopy. jalopy, Jalopy is an easy to configure source code format program that detects and fixes a large number of custom bugs in Java code. Jalopy is more like a code-builder than an inspector. Jalopy's plug-ins now support most Ides, and most are seamles

Effective Java (4) enhance the ability of non-instantiation through private constructors

I. BACKGROUND For some tool classes such as Java.lang.Math, Java.util.Arrays, and so on, they contain only static methods and static field fields, and there is no point in instantiating such a class. But in practice, such a class can be instantiated just like any other class without any special handling. Ii. Methods of Treatment The constructor is set to private so that the outer class cannot instantiate the class and throw an exception directly in the private constructor to avoid being insta

Effective generic parts in Java

Tag: Is the CER object type call error arraylist conversion extend compilerToday will be effective Java (second edition) in the generic part of the reading, deep understanding of their own generic mastery of how unskilled, or need more practice.Say less nonsense, on the point of focus:1. Do not use prototypesLike what:New ArrayList ();When you use the list reference to point to other lists with generics, th

"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

Total Pages: 15 1 .... 7 8 9 10 11 .... 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.