[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
--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.
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
--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.
--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 of
BigDecimal 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.
--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.
-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.
--For string concatenation, use StringBuilder
when there are many items.
--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 the
java.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.
--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.
--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.
—— 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.
--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.
--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
--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