Sometimes my teammate Gerrit teases me “Don’t you know this and that as a Java Champion?”… Of course I don’t. Honestly, the older I get, the better I know that I know nothing.
This week has been interesting. At first, I stumbled upon something like this
A shortcut to System properties
public interface Stuff { String SOMETHING_ENABLED_KEY = "stuff.is.enabled"; static boolean isSomethingEnabled() { return Boolean.getBoolean(SOMETHING_ENABLED_KEY); } } |
And I was like: “How the hell do I set SOMETHING_ENABLED_KEY
too true?!”, only having Boolean#parseBoolean(String s) in mind, which “parses the string argument as a boolean.”
Boolean#getBoolean(String s)
actually goes to the Systems properties and checks for the existence of a property named s
as described in the docs: “Returns true if and only if the system property named by the argument exists and is equal to the string ‘true’.”
I honestly didn’t expect such a shortcut in a wrapper class. To my surprise, those exists for other wrappers like java.lang.Long
, too.
See related twitter thread:
I'm doing Java for so long, but sometimes I'm still surprised: java.lang.Boolean#getBoolean(String) goes to system properties. I was so convinced that is an alias for parseBoolean(String)
— Michael Simons (@rotnroll666) July 2, 2019
But, the week just got more interesting.
Things that are and aren’t enums (aka isEnum()
)
See this gist right here: EnumTests.java. To understand this, you have to know a bit more about EnumTypes.
An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it.
I guess many people stop reading there and don’t go further into the planet example.
Enums are very versatile things. They can work as strategies and are one of the easiest and actually safest to use singletons in Java. They can also implement interfaces. I use this here in ParameterConversion.java to encapsulate a strategy and providing one defined instance only. Joshua Bloch talks about this in Effective Java Third Edition in detail.
But anyway, that’s not the point. Instances of an enum (that is, one of the “constants”) can overwrite the enums methods. As soon as you do this, they are realised as an anonymous, inner class. And with it, asking for EnumType.ENUM_CONSTANT.class.isEnum()
will return false.
Brian Goetz is totally right here, even though it’s embarrassing for me:
Actually, it's actually kind of obvious if you think about how reflection works. This is just straight-up "user doesn't understand what question is being asked." There's nothing very strange or surprising here, just a fuzzy understanding of what `foo.getClass().isEnum()` means.
— Brian Goetz (@BrianGoetz) July 3, 2019
I could have asked “EnumType.class, are you an enum?” and that holds correctly true. The enum constants however doesn’t promise that. If you want to check wether a thing is an instance of an enum type, than just do this: EnumType.ENUM_CONSTANT instanceof Enum>
or if you have an object of unknown type, do this Enum.class.isAssignableFrom(aThing.getClass())
I learned so much from the answers to my tweet here, you should check them out:
Dafuq Java? https://t.co/jtW0c55P9G enum or not?
— Michael Simons (@rotnroll666) July 3, 2019
Twitters UI messes the threading up a bit, so I highlight that one here, from Joseph Darcy:
FYI, for some of the history behind Class.isEnum see https://t.co/BKPNuGe0Qd (please excuse less than ideal blogging system conversion artifacts).
— Joseph Darcy (@jddarcy) July 3, 2019
This old Oracle Blog is a great read: So you want to change the Java Programming Language…
And now what?
On to the next week, onto more learning, more development. Don’t let titles or honours impress you too much. We all learn new stuff, even in old things, each day. And I do think that’s a very good thing.
Title photo: Kimberly Farmer
No comments yet
One Trackback/Pingback
[…] >> Never Not Learning… [info.michael-simons.eu] […]
Post a Comment