BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News 11 Puzzles from JDK11: Hanno Embregts on Java Certification at Devoxx UK

11 Puzzles from JDK11: Hanno Embregts on Java Certification at Devoxx UK

This item in japanese

Bookmarks

As part of his talk at Devoxx UK, Hanno Embregts shared 11 "crazy things" he learned on his path towards Oracle Java 11 certification. Even though he had a career spanning almost one and a half decades, these Java curiosities still needed to be explored before he could adopt them as common practice.

When asked what would be the most important learning from his certification, Embregts mentioned:

Even if I felt I was 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.

As developers like to dive into code, the conference session was a slideless 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 needs 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, a RuntimeException will be thrown if the targeted objects are not Comparable objects, i.e. 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 the case of using floating-point numbers (float or double), this will result in POSITIVE_INFINITY.

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

When the compiler cannot find the exact type, it will widen to broader primitive types, and if primitives are 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 a bitwise operator, and 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 the 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 use the @FunctionalInterface annotation.

#1: Passing arguments to method references

Embregts suggest that by starting with the perspective that driving a car each day to work doesn’t make you a professional driver, coding each day might also not allow you to become a professional coder. So, from this perspective, allocating time to more deeply understand the framework and the language used in a developer's day job would help in improving the quality of the written code and avoid tricky bugs connected to the surprising outcome of certain operations. This type of learning can also help you become more productive.

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

About the Author

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT