Java Puzzlers: Number.MIN_VALUE

If I asked you what the largest possible Integer in Java is, you would easily answer 2^31-1, or Integer.MAX_VALUE. Smallest possible Integer? Integer.MIN_VALUE, -2^31.

How about a Long? It’s also a Number, and each Number has MIN_VALUE and MAX_VALUE, and you would be right to refer to them.

Obvious and boring, right?

How about a Double then?

It’s also a Number, and so it has Double.MAX_VALUE, and yes that is the maximum. Minimum? Let me go already, obviously Double.MIN_VALUE!

Nope.

I only learned it today even though I’ve been programming Java for years. Double.MIN_VALUE is actually a positive number. Quick look at docs says it all: It is the smallest positive nonzero value of type double. If you’re really looking for the smallest value of Double, you need to refer to Double.MIN_NORMAL.

Yes, docs are very clear about it. Yes, it is a static constant, so LSP does not really apply (as if Java didn’t violate it all the time). Yet I still think it looks like a duck, it quacks like a duck, but needs batteries. It has different semantics for no good reason, does not follow the principle of least surprise and is plain unintuitive. I add it to my shelf of quirks along with half of Swing, everything around Dates and Calendars, equals and… what am I still doing here by the way?

One thought on “Java Puzzlers: Number.MIN_VALUE

  1. Uhhh, you’ve programmed in JavaScript, and you’re complaining that Java has annoying quirks? That’s like the driver of an Isuzu Trooper pointing out flaws in a Ford F-150. Java is far from perfect, and this Double.MIN_VALUE thing is insane, and navigating the minefield of JTable’s shortcomings is infuriating, but compared to JavaScript, it’s a little slice of nirvana.

Leave a Reply

Your email address will not be published. Required fields are marked *

Spam protection by WP Captcha-Free