effective-java

Effective Java By Joshua Bloch: Item1 - Static Factory Method

北战南征 提交于 2019-11-28 16:53:26
I am reading the Effective Java by Joshua Bloch and I have question about Item1 Static Factory Method . Quote[Bloch, p.7] Interfaces cant have static methods, so by convention, static factory methods for an interface named Type are put in non-instantiable class named Types. For example, the Java Collections Framework, provide unmodifiable collections, synchronized collections, and the like. Nearly all of these implementations are export via static factory methods in one noninstantiable class (java.util.Collections). The classes of the returned objects are all non-public. Ok. When look at the

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

廉价感情. 提交于 2019-11-28 12:40:17
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 changed. The individual domain objects have the id generated once when they are first created, whereas

Heterogeneous Container with two element of same type

我的梦境 提交于 2019-11-28 10:00:17
问题 I am reading Effective Java - Item 29 . It talks about Heterogeneous container , in the example: private Map<Class<?>, Object> favorites = new HashMap<Class<?>, Object>(); public <T> void putFavirite(Class<T> type, T insance) { if(type == null) { throw new NullPointerException(); } favorites.put(type, insance); .... } This pattern parametrises the key instead of values, so you are not limited to a single type, unlike: private Map<Integer, String> favorites .... My question is: what if there

What is the difference between a compile time type vs run time type for any object in Java?

我怕爱的太早我们不能终老 提交于 2019-11-28 06:37:52
What is the difference between compile time and run time type of any object in Java ? I am reading Effective Java book and Joshua Bloch mentions about compile time type and run time types of array instances in Item 26 many times mainly to describe that suppressing cast warnings can be safe sometimes. // Appropriate suppression of unchecked warning public E pop() { if (size == 0) throw new EmptyStackException(); // push requires elements to be of type E, so cast is correct @SuppressWarnings("unchecked") E result = (E) elements[--size]; elements[size] = null; // Eliminate obsolete reference

Why do we need bounded wilcard <? extends T> in Collections.max() method

六月ゝ 毕业季﹏ 提交于 2019-11-27 17:29:35
问题 I've read awesome "Effective Java" by Joshua Bloch. But one example in the books is left unclear to me. It's taken from chapter about generics, exact item is "Item 28: Use bounded wildcards to increase API flexibility" . In this item it's shown how to write the most universal and bulletproof (at the type system point of view) version of the algorithm of selection maximum element from collection using bounded type parameters and bounded wildcard types. The final signature of the static method

how caching hashcode works in Java as suggested by Joshua Bloch in effective java?

感情迁移 提交于 2019-11-27 12:34:39
问题 I have the following piece of code from effective java by Joshua Bloch (Item 9, chapter 3, page 49) If a class is immutable and the cost of computing the hash code is significant, you might consider caching the hash code in the object rather than recalculating it each time it is requested. If you believe that most objects of this type will be used as hash keys, then you should calculate the hash code when the instance is created. Otherwise, you might choose to lazily initialize it the first

Why are readObject and writeObject private, and why would I write transient variables explicitly?

我怕爱的太早我们不能终老 提交于 2019-11-27 11:28:10
I am reading the chapter on Serialization in Effective Java . Who calls the readObject() and writeObject()? Why are these methods declared private ? The below is a piece of code from the book // StringList with a reasonable custom serialized form public final class StringList implements Serializable { private transient int size = 0; private transient Entry head = null; //Other code private void writeObject(ObjectOutputStream s) throws IOException { s.defaultWriteObject(); s.writeInt(size); // Write out all elements in the proper order. for (Entry e = head; e != null; e = e.next) s.writeObject

What is an AssertionError? In which case should I throw it from my own code?

折月煮酒 提交于 2019-11-27 10:13:59
问题 In Item 2 of the "Effective Java, 2nd edition" book, there is this snippet of code, in which the author wants to forbid the empty initialization of an object. class Example { private Example() { throw new AssertionError(); } } The type of exception thrown, is what confuses me here. I don't understand if the AssertionError is thrown just because of an absence of more suited errors or because it should be this way. As I understand, this error is thrown by the framework when an assert statement

What's the difference between raw types, unbounded wild cards and using Object in generics

大城市里の小女人 提交于 2019-11-27 03:58:31
I am reading the chapter on Generics in Effective Java. Help me understand difference between Set , Set<?> and Set<Object> ? The following paragraph is taken from the book. As a quick review, Set<Object> is a parameterized type representing a set that can contain objects of any type, Set<?> is a wildcard type representing a set that can contain only objects of some unknown type, and Set is a raw type, which opts out of the generic type system. What is meant by "some unknown type"? Are all unknown types of type Object ? In that case what is the specific difference between Set<?> and Set<Object>

What is the difference between a compile time type vs run time type for any object in Java?

眉间皱痕 提交于 2019-11-27 01:26:23
问题 What is the difference between compile time and run time type of any object in Java ? I am reading Effective Java book and Joshua Bloch mentions about compile time type and run time types of array instances in Item 26 many times mainly to describe that suppressing cast warnings can be safe sometimes. // Appropriate suppression of unchecked warning public E pop() { if (size == 0) throw new EmptyStackException(); // push requires elements to be of type E, so cast is correct @SuppressWarnings(