effective-java

Why are the angle brackets before the return type omitted sometimes from the definition of a generic method

♀尐吖头ヾ 提交于 2021-02-04 15:09:30
问题 I was reading Effective Java chapter 5 about generics, in particular the items on preferring generic methods. I noticed that sometimes the type parameter(between angle brackets) in the method declaration before the return type is sometimes omitted. There are many cases like that, but for example on page 135 of the second edition: public void popAll(Collection<E> dst) { while (!isEmpty()) dst.add(pop()); } On the other hand, I have seen similar generic methods with the declaration public <E>

Public methods vs public APIs

微笑、不失礼 提交于 2020-06-29 04:16:35
问题 In clean code book, there is a point that says "Javadocs in Public APIs". And the same for Effective java book, there is this item : "Item 56: Write doc comments for all exposed API elements". So this is my question "Are all public methods considered public APIs?" 回答1: A public API is exposed by means of public methods. A public method alone may not be an API. If the the question is if an API can be made of methods in "normal" classes that are not REST points, the answer is yes. 回答2: They are

Public methods vs public APIs

冷暖自知 提交于 2020-06-29 04:15:32
问题 In clean code book, there is a point that says "Javadocs in Public APIs". And the same for Effective java book, there is this item : "Item 56: Write doc comments for all exposed API elements". So this is my question "Are all public methods considered public APIs?" 回答1: A public API is exposed by means of public methods. A public method alone may not be an API. If the the question is if an API can be made of methods in "normal" classes that are not REST points, the answer is yes. 回答2: They are

Why is it safe to suppress this unchecked warning?

眉间皱痕 提交于 2020-01-03 09:03:14
问题 Consider the UnaryFunction interface defined in Effective Java generics chapter . public interface UnaryFunction<T> { T apply(T arg); } and the following code for returning the UnaryFunction // Generic singleton factory pattern private static UnaryFunction<Object> IDENTITY_FUNCTION = new UnaryFunction<Object>() { public Object apply(Object arg) { return arg; } }; // IDENTITY_FUNCTION is stateless and its type parameter is // unbounded so it's safe to share one instance across all types.

Java: When to add readObjectNoData() during serialization?

99封情书 提交于 2019-12-31 10:44:08
问题 I am reading the serialization chapter in "Effective Java". I am trying to understand the below paragraph in the book. If you implement a class with instance fields that is serializable and extendable,there is a caution you should be aware of. If the class has invariants that would be violated if its instance fields were initialized to their default values (zero for integral types, false for boolean, and null for object reference types), you must add this readObjectNoData method to the class:

Using auto generated id of Hibernate entity object in the equals and hashcode methods

我怕爱的太早我们不能终老 提交于 2019-12-29 00:45:11
问题 Lovely equals and hashcode, all the theory is here and also here I have taken the decision to use the auto-generated id within equals() and hashcode() in a number of my hibernate entity/domain objects. However, a number of websites say you should never do this due to the risk of persisting an object to the database for the first time whilst it is in the process of being compared or using hashcode. My point of view is that in most use cases this is much more unlikely than any other field being

Immutable in java

℡╲_俬逩灬. 提交于 2019-12-23 17:15:55
问题 In Effective Java, Bloch recommends to make all the fields final in making an object immutable . Is it necessary to do so ? Won't just not giving accessor methods make it immutable. For example class A { private int x; A (int x) { this.x = x; } } The above class is immutable even if I don't declare x as final right ? Am I missing something ? 回答1: In addition to @Bozho's point, declaring a field as final means that it can be safely accessed without any synchronization. By contrast, if the

Why is there a need to override hashcode if I override the 'equals' method in Java?

谁都会走 提交于 2019-12-23 09:34:46
问题 I know there is a need to override hashcode whenever the equals method is overridden in Java. That is merely a contract. I am trying to understand the logic behind this. I was reading *Effective Java by Joshua Bloch, and I came across this code (Item 9, page 45): import java.util.HashMap; import java.util.Map; public final class PhoneNumber { private final short areaCode; private final short prefix; private final short lineNumber; public PhoneNumber(int areaCode, int prefix, int lineNumber) {

Effective Java Item 11: Override clone Judiciously

不想你离开。 提交于 2019-12-22 11:03:40
问题 For a class with an array field Josh says if the clone method merely return super.clone(), the resulting class instance will have the correct values in primitive fields, but its array field will refer to the same array as the original class instance. Modifying the original will destroy the invariants and vice-versa. He used the example of custom Stack implementation, I am using a simple Student class class Student implements Cloneable { private String name; private int age; private int[]

Enum Types as explained in Effective Java by Joshua Bloch

北慕城南 提交于 2019-12-21 15:08:25
问题 Please see this link. Regarding Enums, Mr. Bloch says Java’s enum types are classes that export one instance for each enumeration constant via a public static final field. I read the Enum Class documentation but there was no public static final field , then how does the above statement hold true. Please explain. Thanks 回答1: Create a Test.java file and write Test enum : public enum Test { Hello } compile this class: javac Test.java ,and use javap Test to get the compiled class: public final