effective-java

Effective Java Item 47: Know and use your libraries - Flawed random integer method example

半城伤御伤魂 提交于 2019-11-30 13:14:47
问题 In the example Josh gives of the flawed random method that generates a positive random number with a given upper bound n , I don't understand the two of the flaws he states. The method from the book is: private static final Random rnd = new Random(); //Common but deeply flawed static int random(int n) { return Math.abs(rnd.nextInt()) % n; } He says that if n is a small power of 2, the sequence of random numbers that are generated will repeat itself after a short period of time. Why is this

What does “Recursive type bound” in Generics mean?

感情迁移 提交于 2019-11-30 06:55:49
问题 I am reading the chapter on Generics from Effective Java[Item 27]. There is this paragraph in the book: It is permissible, though relatively rare, for a type parameter to be bounded by some expression involving that type parameter itself. This is what’s known as a recursive type bound. and this: // Using a recursive type bound to express mutual comparability public static <T extends Comparable<T>> T max(List<T> list) {...} What is recursive type bound and how does the above piece of code help

Effective Java Item 47: Know and use your libraries - Flawed random integer method example

不想你离开。 提交于 2019-11-30 06:37:38
In the example Josh gives of the flawed random method that generates a positive random number with a given upper bound n , I don't understand the two of the flaws he states. The method from the book is: private static final Random rnd = new Random(); //Common but deeply flawed static int random(int n) { return Math.abs(rnd.nextInt()) % n; } He says that if n is a small power of 2, the sequence of random numbers that are generated will repeat itself after a short period of time. Why is this the case? The documentation for Random.nextInt() says Returns the next pseudorandom, uniformly

Why are public static final array a security hole?

寵の児 提交于 2019-11-29 23:37:19
Effective java says: // Potential security hole! static public final Thing[] VALUES = { ... }; Can somebody tell me what is the security hole? Declaring static final public fields is usually the hallmark of a class constant. It's perfectly fine for primitive types (ints, doubles etc..), and immutable classes, like strings and java.awt.Color . With arrays, the problem is that even though the array reference is constant, the elements of the array can still be changed, and as it's a field, changes are unguarded, uncontrolled, and usually unwelcome. To combat this, the visibility of the array

Builder Pattern: which variant is preferred? [closed]

泄露秘密 提交于 2019-11-29 20:23:15
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . I was going through Effective Java book , and creating notes for my future reference , i came across Builder Pattern. Well i

Heterogeneous Container with two element of same type

我们两清 提交于 2019-11-29 15:55:31
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 are two elements of same type that needed to be added to the Map , i.e. two String , is this pattern

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

£可爱£侵袭症+ 提交于 2019-11-29 05:38:58
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 written looks like this: public static <T extends Comparable<? super T>> T max(List<? extends T> list)

What does “Recursive type bound” in Generics mean?

岁酱吖の 提交于 2019-11-28 23:13:24
I am reading the chapter on Generics from Effective Java[Item 27]. There is this paragraph in the book: It is permissible, though relatively rare, for a type parameter to be bounded by some expression involving that type parameter itself. This is what’s known as a recursive type bound. and this: // Using a recursive type bound to express mutual comparability public static <T extends Comparable<T>> T max(List<T> list) {...} What is recursive type bound and how does the above piece of code help achieve mutual comparability? What is recursive type bound This: <T extends Comparable<T>> Note that

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

会有一股神秘感。 提交于 2019-11-28 20:54:35
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 time hashCode is invoked (Item 71). It is not clear that our PhoneNumber class merits this treatment,

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

人盡茶涼 提交于 2019-11-28 17:09:24
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 fails. Also, in the javadoc it's just written [An AssertionError is] Thrown to indicate that an