DevNexus 2022 Live Blog Table of Contents

DevNexus is back in person! This is my second in person event since COVID. ”code with soul” logo and ”welcome back to conferences” on signage

COVID handling

Vaccines (or tests) are required. In theory, masks are required in rooms but not in the hallways. Looking around they keynote, I think less than 10% are wearing masks. Had about 2400 people in person last time it was live and about half that this year. Sessions kept the doors open. Good for air. Bad for noise (some session rooms are right off the exhibit hall)

Tuesday

  • I got here right after lunch and spent two hours at the JUG Leader summit. I didn’t take notes, but I got the updates from Oracle (remember JavaOne is back in person in Vegas this year!). We also had some good unconference sessions. They used the fishbowl format where three people sat in chairs with an empty chair. To speak, you take an empty chair and someone has to get up

Wednesday

Thursday

Java CompactNumberFormat Bug or Feature?

Java 12 introduced a new CompactNumberFormat class, which anyone studying for the new Java 17 1Z0-829 OCP certification should know. It’s really cool utility feature, helping to shorten lengthy number values into shorter forms for common usage. It supports a Style setting, SHORT (1M) vs LONG (1 million), as well as rounding, and many other features. Generally speaking, it rounds the value to the first human-readable 3-digit tuple, formats it, and then adds a label depending on style/locale.

Let’s take a look at an example:

var shortCNF = NumberFormat.getCompactNumberInstance(Locale.US,
   Style.SHORT);
var longCNF = NumberFormat.getCompactNumberInstance(Locale.US,
   Style.LONG);
System.out.print(shortCNF.format(15_300));          // 15K
System.out.print(longCNF.format(15_300));           // 15 thousand
System.out.print(shortCNF.format(124_000_200));     // 124M
System.out.print(longCNF.format(124_000_200));      // 124 million
System.out.print(shortCNF.format(4_834_000_000.0)); // 5B
System.out.print(longCNF.format(4_834_000_000.0));  // 5 billion

Useful stuff, right? Notice the last two examples rounded the value up to 5 bllion? Rounding (which can be disabled) is enabled by default. Well, while writing some really tricky practice exam questions for our upcoming Java OCP 17 Practice Test Book, I discovered something rather odd:

var shortCNF = NumberFormat.getCompactNumberInstance(Locale.US,
   Style.SHORT);
var longCNF = NumberFormat.getCompactNumberInstance(Locale.US,
   Style.LONG);
System.out.print(shortCNF.format(999_999));   // 1000K (this is weird)
System.out.print(longCNF.format(999_999));    // 1000 thousand (this is weird)
System.out.print(shortCNF.format(1_000_000)); // 1M
System.out.print(longCNF.format(1_000_000));  // 1 million
System.out.print(shortCNF.format(1_999_999)); // 2M
System.out.print(longCNF.format(1_999_999));  // 2 million

Notice the issue? If the CompactNumberFormat rounds up and enters a new range (thousand to million, million to billion, etc), it doesn’t adjust the labels or values. The first two sets of values should print 1M and 1 million, but the rounded value prints 1000K and 1000 thousand instead. While I used Locale.US for reproducibility, this isn’t required. It appears when you use other locales, and other ranges. For instance, 999_999_999 formats as 1000M, instead of 1B. I validated on Oracle’s latest release of Java 17.0.2.

So.. is this a bug or a feature? It partially depends how you read the unicode spec the Java feature was based on. The spec covers formatting rules and order of operation, but it doesn’t provide as much insight on how rounding is supposed to be handled in this particular situation.

I believe this is a bug because:

  • No one would ever expect (or want) to see one million written as 1000 thousand or 1000K. If you saw that on a website or mobile app, you’d likely report it as a bug. (If you’re a developer using this feature, you would probably then be told to stop using this library altogether!)
  • If it is working as designed, then the spec has a problem. The only work-around for someone who wants to use CompactNumberFormat without encountering this issue is to either disable rounding, or round the value ahead of time. In either situation, the utility of using the CompactNumberFormat feature drops precipitously.

To me, it’s a bug…. or a feature that renders CompactNumberFormat not suitable for practical use. With that in mind, I opened a bug ticket JDK-8281317 with Oracle to address the issue. I will update this page when I get a response!

Side note: On February 6, I created a Twitter poll and interestingly enough the correct answer of 1000K was the least selected option! Certainly, not an intuitive implementation!

Levels of Helpfulness in Helpful Null Pointers

Java introduced Helpful Null Pointers. Let’s take a look at a few versions using this class:

public class Helpful {
  private String name;
  private Helpful(String name) {
    this.name = name.toUpperCase();
  }
  public static void main(String... args) {
     new Helpful(null);
  }
}

Three approaches

First lets’ run this using Java 11. We get the stack trace

Exception in thread “main” java.lang.NullPointerException
at Helpful.(Helpful.java:4)
at Helpful.main(Helpful.java:7)

Well that’s not helpful. I have to go look at line 4 to even have a clue what is going on. Now let’s run it in Java 17. (Because I’m sticking with Long Term Support versions)

Exception in thread “main” java.lang.NullPointerException: Cannot invoke “String.toUpperCase()” because “<parameter 1>” is null
at Helpful.(Helpful.java:4)
at Helpful.main(Helpful.java:7)

That’s more helpful. It tell us the toUppercase() call is the problem. And that a constructor was the cause. It would be even more helpful to know the name of the variable. This requires a command line flag. Compile with -g:vars and you get this when running

Exception in thread “main” java.lang.NullPointerException: Cannot invoke “String.toUpperCase()” because “name” is null
at Helpful.(Unknown Source)
at Helpful.main(Unknown Source)

Conveniently, both Eclipse and IntelliJ turn on this flag by default.