Missing value in Java

后端 未结 11 2825
慢半拍i
慢半拍i 2021-02-20 11:51

What is the statement that can be used in Java to represent a missing value of a variable. for instance I want to write a code:

if (a>=23)
 income = pay_rate;         


        
11条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-20 12:14

    As mentioned in other ans here, you can use a boxed type. But I ll recommend look for the valid values for your data and choose any invalid value for missing. In your specific case of income, which cannot be negative, so you can choose -1.

    The reason to avoid boxed type is performance. I am not against using them but if you can work without them then there is no need to use them.

    For the cases where most of +ve and -ve values are valid values, use eitherInteger.MAX_VALUE or Integer.MIN_VALUE as your missing value. You ll just loose one value of all the available value range.

    NOTE: Auto-boxing (converting implicitly from primitive to boxed type and vice-versa) is a good feature but can lead to some performance issues which are difficult to debug and find.

提交回复
热议问题