Effective Java 3rd Edition Chapter 9 Program General

[Effective Java 3rd Edition](https://www.amazon.co.jp/Effective-Java-%E7%AC%AC3%E7%89%88-%E3%] 82% B8% E3% 83% A7% E3% 82% B7% E3% 83% A5% E3% 82% A2% E3% 83% BB% E3% 83% 96% E3% 83% AD% E3% 83% 83% E3% 82% AF-ebook / dp / B07RHX1K53) has a Kindle version, so I will summarize it.

Previous: Effective Java 3rd Edition Chapter 8 Methods Next: Creating

Item 57 Minimize the scope of local variables

--The most powerful technique for minimizing the scope of local variables is to declare them the first time they are used. (Do not initialize at the beginning of the block) --Almost all local variable declarations should include an initializer. If you don't have the information you need to initialize, you should postpone the variable declaration. --Loop variables can be declared in both for loop and for-each loop, and the scope is limited to the required area.

for (Element e : c) {
  //Do something with e
}

for (Iterator<Element> i = c.iterator; i.hasNext(); ) {
  Element e = i.next();
  //Do something with e and i
}

--Make the method smaller to make it more focused. If there are two processes in the method, the scope will be smaller if the methods are separated.

Item 58: Use a for-each loop rather than a conventional for loop

for (Suit suit : suits) {
  for (Rank rank : ranks) {
    deck.add(new Card(suit, rank));
  }
}

--For-each is superior to traditional for loops in terms of clarity, flexibility, and bug prevention. --There is no performance penalty. --Situation where for-each cannot be used --Destructive filtering: When looping while deleting elements --Conversion: When replacing some or all of the elements --Parallel iteration: When looping in parallel

Item 59 Know the library and use the library

--As of Java7, Random should no longer be used, ThreadLocalRandom should be used. High quality and fast. --Advantages of using the standard library —— You can use the knowledge of experts and the experience of the people you used before. --Do not waste time. You should spend time on your application. --Performance may improve over time. --Over time, new features will be added to the library. --Your code is accepted by a large number of developers.

Item 60 Avoid floats and doubles if you need an accurate answer

--float type and double type are not suitable for monetary calculation. -Because it is impossible to accurately represent 0.1. --Use BigDecimal type, ʻint, longfor monetary calculation. --The disadvantage ofBigDecimal type is that it is inconvenient and slow to calculate. --Alternatives use ʻint, long, and calculate all in cents instead of dollars. ―― Use up to 9 digits for ʻint, up to 18 digits for long, and use BigDecimal` for more.

Item 61 Select a basic data type over a boxed basic data type

--Differences between basic data types and boxed basic data and types --The data type has only values, and the boxed basic data instance has a different identity than the value. --The basic boxed data can have null. --Basic data types are efficient in terms of time and space. --It is almost wrong to apply the == operator to the boxed basic data. --Usage of basic boxed data --Elements, keys, values in the collection. --Parameterized type and method type parameters.

Item 62 Avoid strings where others are more appropriate

-Character types are designed to represent text and play their part perfectly, but they are poor alternatives to other value types. --If you have a suitable value type, whether it's a basic data type or a reference type, you should use that type.

Item 63 Beware of string concatenation performance

--For string concatenation, use StringBuilder when there are many items.

Item 64 Refer to the object on the interface

--All parameters, return values, variables and fields should be declared using the interface type, if the appropriate interface type exists. --If you want to switch the implementation, just change the class name that specifies the constructor. (Or use another static factory method) --If the appropriate interface does not exist, it is appropriate to refer to the object in the class instead of the interface. --Value classes such as String, ʻInteger --If the object belongs to a class-based framework, it is desirable to refer to it in the associated base class, which is generally an abstract class. Many of thejava.io, such as ʻOutputStream, belong to this category. --If the interface is implemented, but the class provides additional methods that the interface does not have. For example, the PriorityQueue class with the comparator method. However, it should be rare.

Item 65 Use interface rather than reflection

--The price of reflection --Lose all the benefits of compile-time type checking. Attempting to call a non-existent or inaccessible method will fail at program execution. --The code that accesses by reflection is awkward and verbose. Difficult to read. --Performance is poor. --If you write a program that must work with a class that is not known at compile time, use reflection only for instances of the object as much as possible, and access the object using interfaces and superclasses that are known at compile time. should.

Item 66: Use native methods with caution

--The Java Native Interface (JNI) enables Java applications to call native methods, which are methods written in native programming languages such as C and C ++. --Usage of native method --Provides access to platform-specific mechanisms such as registries and file locking. --Provides access to existing native code and includes legacy libraries. --Performance improvement. --The JVM implementation is getting faster and it is not recommended to use native methods. --Disadvantages of native methods --It's not safe. --Low portability and difficult to debug. --Calling native methods is costly and can reduce performance.

Item 67 Carefully optimize

—— Try to write better programs than fast ones. If you write a good program, speed is the result. --After building the system, measure the performance. --If it's not fast, find out the cause with the help of the profiler. --Variable public types may unnecessarily require a defensive copy.

Item 68: Observe the generally accepted naming convention

--There are typographical and grammatical naming conventions. --The print naming convention is clear with only a handful. --The grammatical naming convention is complicated and ambiguous.

Print naming convention

--Package name, module name ――It should have elements separated by periods and be hierarchical. --Consists of lowercase letters and rarely numbers. --Should start with an internet domain name, with the elements reversed. --Elements should be short and no more than 8 characters. --The abbreviation is recommended. --Class name, interface name, enum name --Starts with a capital letter. (Upper camel case, Pascal case) --Abbreviations should be avoided. --Method name, field name --Should start with lowercase (lower camel case) --Constant field --All capital letters, separated by underscores. --static final, or immutable field --Local variables --Same as the member name, but abbreviations are allowed. --Type parameter name ――Mostly one character. --T Arbitrary type --ʻE Collection element type --K`` V Map and key values --X exception --R` Return value

Literary naming convention

--Class name, enum name --Singular nouns, noun phrases --Utility classes that cannot be instantiated --Multiple nouns --Interface --Adjectives ending with able, ible --Annotation type --All part of speech is used --Method that does some processing --Verbs, verb phrases --Methods that return a boolean value begin with is, has. --Methods that are not boolean values are nouns, noun phrases, and verb phrases that start with get --A method that converts the type of an object and returns another type --Called toType, toString, toArray --A method that returns a view with a type different from the type of the receiver object --Called asType, asList --A method that returns basic data with the same value as the object for which the method was called --Called typeValue. intValue --Common name with static factory method - from、of、valueOf、instance、getInstance、newInstance、getType、newType

Recommended Posts

Effective Java 3rd Edition Chapter 9 Program General
Effective Java 3rd Edition Chapter 5 Generics
Effective Java 3rd Edition Chapter 8 Methods
Effective Java 3rd Edition Chapter 4 Classes and Interfaces
Effective Java Chapter 2
Effective Java Chapter 6 34-35
Effective Java Chapter 4 15-22
Effective Java Chapter 3
Effective Java 3rd Edition Chapter 3 Methods Common to All Objects
effective java 3rd summary
Effective Java 3rd Edition Section 85 Choose Alternatives Over Java Serialization
What has changed between Effective Java 2nd Edition and 3rd Edition
[Read Effective Java] Chapter 2 Item 7 "Avoid Finalizers"
[Read Effective Java] Chapter 3 Item 8 "When overriding equals, follow the general contract"
Effective Java 2nd Edition Related Matters Survey Summary
[Read Effective Java] Chapter 3 Item 10 "Always Override toString"
I tried to explain Effective Java 3rd edition "almost all chapters" in "easy-to-read Japanese".
[Read Effective Java] Chapter 3 Item 12 "Considering Implementation of Comparable"
[Read Effective Java] Chapter 2 Item 6 "Remove obsolete object references"
Updates from Effective Java Third Edition 2nd Edition Personal Notes
Java Performance Chapter 1 Introduction
I will write an excerpt of what I found interesting while reading Effective Java 3rd Edition
Builder pattern (Effective Java)
Java Performance Chapter 3 Java Performance Toolbox
[Java] Calculator program creation 1