How to present the nullable primitive type int in Java?

后端 未结 13 1027
灰色年华
灰色年华 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 13:57

    Another option is to have an associated boolean flag that indicates whether or not your year value is valid. This flag being false would mean the year is "unknown." This means you have to check one primitive (boolean) to know if you have a value, and if you do, check another primitive (integer).

    Sentinel values often result in fragile code, so it's worth making the effort to avoid the sentinel value unless you are very sure that it will never be a use case.

提交回复
热议问题