7

11 Puzzlers From JDK11

 2 years ago
source link: https://www.infoq.com/news/2022/05/11-puzzlers-jdk11/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

11 Puzzlers From JDK11

May 12, 2022 3 min read

JDK 11 adoption increases by day passing, by the end of 2021 it had reached parity with JDK 8.

As part of his talk given in front of Devoxx UK audience, Hanno Embregts shares 11 crazy things he learned on his path towards Oracle Java 11 certification. Even though having behind a career spanning of almost one decade and a half, these Java curiosities still needed to be ironed out while preparing becoming common practice afterwards. When asked what would be most important learning from his certification, Embregts mentioned:

Even if I felt I am on top of my coding game, I realised that as professional developers we need to invest time into improving our knowledge of the tools that we use. That will improve our productivity.

And as developers like to code and dive into code, the presentation was a slideles presentation focused on 11 UTs each of them providing the "puzzles".

#11: Array type definitions

  • Combining local type inference with [] would not compile 
  • C type arrays definition need to be duplicated for each variable, it is not replicated:

#10: Sorting a stream would work just on Comparable objects

If you are trying to sort a stream of objects, I will throw a RuntimeException if the targeted objects are not Comparable objects, hence implementing the compareTo(...) method.

#9: Just the containing interfaces can access static methods inside them

Trying to access the static method from a class would not compile.

#8: Creating anonymous subclasses in an enum to override the functionality of the enum

Not overriding methods that were defined in enums, could potentially generate unexpected behavior. In order to ensure that the desired effect is accomplished, the methods can and need to be overridden in anonymous inner classes.

#7: Depending on the type, division by zero would have a different behavior

Trying to divide a number by 0 in case of int or long would throw an

ArithmeticException, but in case of using floating point numbers(float or double) will result into POSITIVE_INFINITY.

#6: Method overloading parameters priority vary depending on the types used

When not finding the exact type it will widen to broader primitive types and if primitives or not available it will try to match boxed primitives or varargs.

#5: Switch statements can have confusing results, if they try to squash multiple cases:

Usage of the "|" to squash two different cases might have a surprising result as it acts as bitwise operator, the result of the operation being used by the switch to match in the appropriate category.

#4: Equality when dealing with cloned arrays or immutable collections arrays or immutable

The Java clone operation is shallow hence in case of arrays it would point to the same objects as the ones contained in the initial array or collection. In this situation, comparing the two arrays by using "==" will yield false but using Object.equals(...) would yield true.

#3: Numerical wrapper objects

For optimization purposes Java caches numbers in the -128 to 127 range. So numbers in this range would always resolve to the same reference when boxing.

#2: Functional interface

A functional interface in Java should have just one method and to use the @FunctionalInterface annotation.

#1: Passing arguments to method references

Starting with the perspective that driving a car each day to work, doesn’t make you a professional driver. Coding each day might not allow you to become a professional coder. So, from this perspective allocating time to deeper understand the framework and the language mainly used would help in improving the quality of the written code and avoid tricky bugs connected to the surprising outcome of certain operations and also help you become more productive.

About the Author

Olimpiu Pop

Show more

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK