Posts

Showing posts from December, 2022

The "exposes" Keyword in Java

Don't worry. You have not been sleeping under a rock for the past few years. There is no such keyword in Java. It's just something I wish for Christmas. It's great that Java modules finally allow us to truly seal off our carefully crafted API surfaces, without clients being able to crack them open anyway using reflection . If a module does not " open" a package to the outside world, there is no way you can access its private and package-private m embers. No more reflectively setting a private field's accessible flag to true and force it to yield or change its value. Unfortunately, you can also no longer use reflection to dynamically read/write public fields or execute public methods. It does not matter whether the fields and methods hail from an exported or non-exported package. As long as the module does not also open the package, any reflection is off limits. In fact you no longer have any self-contained (purely programmatic) way of knowing what publi...

LinkedList - Lost Opportunity?

Image
I've always found LinkedList a bit of a strange bird among the Collections Framework classes. By rights it should probably come right after ArrayList , HashMap , HashSet and LinkedHashMap as a general-purpose collection class, but it never quite provided the functionality I needed — and expected from a linked list implementation. Internals It seems like the makers of LinkedList were very intent on emphasising its queue-like potential and it is easy to see why. A linked list consists of a series of "nodes" . A node contains a value, a reference to the previous node and a reference to next node. In addition, a linked list maintains a reference to the first node and a reference to the last node. With this setup, queue-like operations like push, pop, shift and unshift become cheap and simple. To push 54 onto the list, wire it up to 13 and make tail point to 54. To pop 13 off the list, just make tail point to 33 and nullify the reference from 33 to 13. Using a linked l...