Joshua Bloch serves up a few Java Puzzlers as an appetizer before and as dessert after the main course on Effective Java.
Organized By: Silicon Valley Web JUG - Van Riper and Kevin Nilson
Hosted and Sponsored By: Google
Video By: Marakana
36. Summary, in Tabular Form Foo<?> ( Independent of T ) Foo<? e xtends T> ( Covariant in T ) No Foo<? s uper T> ( Contravariant in T ) Foo<T> ( Invariant in T ) Yes No Yes Parameter C onsumes T Instances? Input Parameter P roduces T Instances?
37. Filling in The Blanks Foo<?> ( Independent of T ) Foo<? extends T> ( Covariant in T ) No Foo<? super T> ( Contravariant in T ) Foo<T> ( Invariant in T ) Yes No Yes Parameter Consumes T Instances? Parameter Produces T Instances?
76. Effective Java TM : Still Effective, After All These Years Joshua Bloch
Notas del editor
The twisted cord illusion , also known as the Fraser figure , was described by psychologist James Fraser in 1908. The letters “ C AFE babe ” are set straight and true; the perceived tilt is illusory.
Todorovic’s gradient chessboard illusion.
But enough of pleasantries. It’s time for Effective Java
This is the third talk I’ve given on the second edition. The first concentrated on generics, the second on enums. This one is a bit of dog’s breakfast.
Most of this material has not been included in previous talks, but the first two Items are repeats, because I think they bear repeating
Probably the hardest part of generic types in Java is wildcards. They allow you to increase the flexibility of your APIs, but they’re confusing. I’m going to teach you a mnemonic that cuts through the confusion. Transition: Properly used, wildcards can give you the flexibility that you’re used to from arrays, coupled with the type-safety of generics. So how do you use wildcards properly?
It’s simply, really: just use your PECS!
Here’s how the declarations would look without wildcards. But these declarations aren’t very flexible. The src and dst types have to match the stack is exactly.
And what do these wildcard declarations buy you?
And what do these wildcard declarations buy you?
Java’s type inference has its limitations, and our revised union type signature demonstrates one.
Here’s our mnemonic in tabular form. If you’re using a parameter to produce T instances, declare it Foo<? extends T>; if you’re using it to consume T instances, declare it Foo<? super T>. But the table raises a question: what if you’re using a parameter to produce and consume T instances, say adding and removing T’s from a collection, or neither producing nor consuming T’s, say removing elements from a collection.
As you can see from this expanded table, you can’t use wildcards if you’re both adding and removing T’s: the intersection of Foo<? extends T> and Foo<? super T> is Foo<T>, and that’s the type you have to use if you’re both producing and consuming T’s. If you’re neither producing nor consuming T’s, you can accept a Foo of any type, which is an unbounded wildcard.
We call this mind-expanding pattern the Typesafe Heterogeneous Container, or THC for short. The idea is very simple. … Let’s take a look at a complete example.
Here we have a data type that allows you to store a few of your fa-vo-rite things. In this example, the Favorites instance take the part of the database row, and the Class object takes the part of the column Now let’s take a look at the implementation. It’s surprisingly simple!
Notice that the type of the map does not ensure the required relationship between key and value, but that’s OK. The cast method is the dynamic analog of Java’s cast operator. It simply checks that its argument is an instance of the type represented by the Class object. If so, it returns the argument; otherwise it throws a ClassCastException.
As a reward for suffering all that complicated stuff, the next two sections are light and fluffy
Go through code. This works, but you can do much better!
One thing about varargs is that it allocates an array under the covers. Normally this doesn’t matter much, but it can matter in performance critical situations. You can use an idiom very similar to this one to deal with the problem.
Of course you can pick the value of n that works best for your application. If you choose it carefully, you almost never pay for the array allocation Mention premature optimization!
Now I want to talk a bit about concurrent collections, and ConcurrentHashMap, in particular
But be careful
Finally, I want to tell you about a serialization pattern that I really like
What exactly is a constant variable? Sounds like an oxymoron, doesn’t it?
Mention: consturctor would do for String, but ident trick is valuable for primitives. Enums often superior