11 puzzles from JDK11
The adoption of JDK 11 is increasing day by day, by the end of 2021 it had reached parity with JDK 8.
As part of his talk to the Devoxx UK audience, Hanno Embregts shares 11 crazy things he learned on his way to Oracle Java 11 certification. Although after a career spanning almost a decade and a half, these curiosities of Java still needed to be ironed out while preparing to become common practice later on. When asked what would be most important to learn from his certification, Embregts mentioned:
Even though I felt like I was on top of my coding game, I realized that as professional developers, we need to invest time in improving our knowledge of the tools we use. This will improve our productivity.
And since developers love to code and dive into code, the presentation was a slider presentation focusing on 11 UTs, each of them providing the “puzzles”.
#11: Array Type Definitions
- Combine local type inference with  would like to do not compile
- The definition of C-type arrays must be duplicated for each variable, it is not replicated:
#10: Sorting a feed would only work on comparable objects
If you try to sort a stream of objects, I’ll throw a RuntimeException if the targeted objects aren’t comparable objects, thus implementing the compareTo(…) method.
#9: Only containing interfaces can access the static methods they contain
Trying to access the static method from a class would not compile.
#8: Create anonymous subclasses in an enumeration to override the functionality of the enumeration
Not overriding methods defined in enumerations could potentially lead to unexpected behavior. In order to ensure that the desired effect is achieved, methods can and should be overridden in anonymous inner classes.
#7: Depending on the type, division by zero would behave differently
Trying to divide a number by 0 in case of int or long would throw a
ArithmeticException, but when using floating point numbers (float or double) will result in POSITIVE_INFINITY.
#6: The priority of method overload parameters varies according to the types used
When it can’t find the exact type, it will expand to broader primitive types, and whether primitives are available or not, it will try to match framed primitives or varargs.
#5: Switch statements can have confusing results if they attempt to override multiple cases:
Using the “|” overwriting two different cases can have a surprising result because it acts like a bitwise operator, with the result of the operation being used by the switch to match the appropriate category.
#4: Equality against cloned arrays or immutable or immutable collection arrays
The Java clone operation is shallow, so in the case of arrays, it would point to the same objects that were contained in the initial array or collection. In this situation, comparing the two arrays using “==” will yield false but using Object.equals(…) will yield true.
#3: Digital Wrapper Objects
For optimization, Java caches numbers between -128 and 127. So numbers in this range would always resolve to the same reference when boxing.
#2: Functional interface
A functional interface in Java should have only one method and use the @FunctionalInterface annotation.
#1: Passing Arguments to Method References
Assuming that driving a car every day to work does not make you a professional driver. Coding every day may not allow you to become a professional coder. Thus, from this point of view, allocating time to deepen the understanding of the framework and the language mainly used would help to improve the quality of the written code and to avoid delicate bugs related to the surprising result of certain operations and would also help you to become more productive.