, 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
:// 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
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
This is the desired result if any of the following conditions are met:Each instance of a class is inherently unique.Does not care if the class provides a "logical equality" test function.The superclass has overridden equals, and the behavior inherited from the superclass is also appropriate for subclasses.Class is private or package-level private, it can be determined that its Equals method is never called.If a class has its own unique "Roggi" concept (unlike the concept of object equivalence),
Why avoid creating unnecessary objects? The process of creating objects is still more cost-intensive, according to the performance tests given in the book, the time consuming to recreate objects is 250 times times the time consumed by reusing objects. The following is a list of scenarios where objects can be reused.1, String type Note that there is a big difference between string s = "as" and string s = new String ("as"), and the second method of creating a string instance will actually produce
(inkfish original, reprint please indicate the source: http://blog.csdn.net/inkfish/)
In order to seek the performance optimization of Java code, search from the internet to use final keyword compile-time inline optimization method, but really effective. The actual test found that not necessarily, even the performance impact of the huge, eventually abandoned the idea of using final optimization.
Test enviro
Recently in the watercress to see some book reviews, very feel, and there is a call effective Java is said to be very good, download a look, really masterpiece, a lot of things are not read, the skill is still shallow, still have to continue efforts. It feels so good to read that it's much better than a grammar-telling book.
The first part of the object is created and destroyed.
Said 5 principles.
1. Rep
Calendar,date every time. Performance is improved in the following ways, and the meaning of the code is more clear.ImportJava.util.Calendar;Importjava.util.Date;ImportJava.util.TimeZone; Public classPerson {Private FinalDate birthday; Private Static FinalDate Boom_start; Private Static FinalDate Boom_end; Static{Calendar gmtcal= Calendar.getinstance (Timezone.gettimezone ("GMT")); Gmtcal.set (1946, Calendar.january, 1, 0, 0, 0); Boom_start=Gmtcal.gettime (); Gmtcal.set (1965, Calendar.january,
There are two ways to get an instance of a class:1. Provide a public constructor (most commonly used).2. Provide a public static factory method (static factory methods).// Static Factory Method Example Public Static Boolean ValueOf (boolean b) { return b? Boolean.TRUE:Boolean.FALSE;}Advantages of the Static factory approach:1. There is a name. When a class requires more than one constructor with the same signature (method signature = method name + parameter list), the static factory method
= servingsize; This. servings = servings; }/** * * @param val * Assign a value to an option * @return Builder object for chain adjustment Use the Assignment function * / PublicBuildercalories(intval) {calories = Val;return This; } PublicBuilderFat(intval) {fat = val;return This; } PublicBuilderSodium(intval) {sodium = Val;return This; } PublicBuilderCarbohydrate(intval) {carbohydrate = Val;return This; }/** * Create a Get object method and pass yourself (Builder) as a parameter * *
exceptions
throws a corresponding exception to the abstract
Each method throws an exception that must have a document
Include information in the detail message that could be failed to replenish
Try to keep the failure atomic
Do not ignore exceptions
Concurrent
Synchronizing access to shared mutable data
Avoid over-synchronization
Executor and tasks take precedence over threads
Concurrency tools take precedence over wait and notify
Documentation
exceptions for programming errors 59th: Avoid unnecessary use of the exception that is examined 60th: try to use the standard exception 61st: throws an exception corresponding to the abstract 62nd: All exceptions thrown by each method must have a document 63rd: contains the failure-capture information in the detail message 64th: Try to keep the failure in the Atomic 65th Bar: Do not ignore exceptions chapter 10th Concurrent 66th: Synchronizing access to shared variable data 67th: Avoid too many
parameterless builder method to generate the immutable object. This builder is the static member class of the class of its builder.public class Person {private string id;private string Name;private string address;public the parameters necessary for static class builder{//Priv Ate final string id;//optional parameter private String name = "";p rivate string address = "";p ublic Builder (String Id) {this. id = ID;} Public Builder name (String name) {this.name = Name;return this;} Public Builder a
I do not know whether we remember in the "Journey to the" in the Lotus Hole in the story of the Treasure, is the Monkey King skillfully grabbed the treasures, the gold and Silver Kok King that chapter. What does this story give us? What is the connection between this story and effective Java? Or continue the style of the article, read the code, read the story.
1import static org.junit.assert.*;2import org.
, they all provide the same interface to the compiler. The Java compiler generates platform-independent bytecode that cannot be run directly on different platforms and must be run through an intermediary converter (JVM).First the Java compiler generates the code that the virtual machine understands (platform-independent bytecode files), and then the VM interprets the code to execute.The JVM is a key part of
Java music player-Lele music (PC Edition) and java music player
Lele music is a java music player developed based on musique. a music player of the android version has been developed before. Now, the android version code is transplanted to the PC version. However, I encountered a problem: If
circumstances to 8 screen around to display. This makes it difficult to read a program or modify a program. It is therefore advisable to extract the block that completes the comparison of independent functions as a single function. A block that accomplishes the same or similar function is extracted independently of a sub-function. It can be found that the simpler the upper-level function is, the more specific work is done by invoking a few sub-functions, the more the underlying function complet
1, Java is an object-oriented language, not simply as a script to use. From the basics, don't start with spring, EE, Hibernate, and EJB.2, do not impetuous, should be solid first to learn the Java language, and then according to the Java itself learning laws, step by step to learn.3. Never start learning from IDE tools (JBuilder, Eclipse, and NetBeans), the
example:What exactly is boxing and unpacking, boxing is that the compiler performs a boxing operation after the underlying type is changed to the corresponding object type, and the reverse is the case. The following blog is a special introduction to Java Boxing and unpacking to share with youHttp://www.cnblogs.com/dolphin0520/p/3780005.html= = and equals I used almost every day, before this I did not care too much about the differences, and later loo
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.