How to present the nullable primitive type int in Java?

后端 未结 13 966
灰色年华
灰色年华 2020-12-03 13:29

I am designing an entity class which has a field named \"documentYear\", which might have unsigned integer values such as 1999, 2006, etc. Meanwhile, this field might also b

相关标签:
13条回答
  • 2020-12-03 14:09

    What's wrong with java.lang.Integer? It's a reasonable solution, unless you're storing very large amounts of this value maybe.

    If you wanted to use primitives, a -1 value would be a good solution as well. The only other option you have is using a separate boolean flag, like someone already suggested. Choose your poison :)

    PS: damn you, I was trying to get away with a little white lie on the objects vs structs. My point was that it uses more memory, similar to the boolean flag method, although syntactically the nullable type is is nicer of course. Also, I wasn't sure someone with a Java background would know what I meant with a struct.

    0 讨论(0)
提交回复
热议问题