Conversion from null to int possible?

前端 未结 6 1928
刺人心
刺人心 2020-12-08 02:05

I know that when I read the answer to this I will see that I have overlooked something that was under my eyes. But I have spent the last 30 minutes trying to figure it out m

6条回答
  •  攒了一身酷
    2020-12-08 02:24

    The problem with autounboxing null values can be really annoying. In your example it's a combination of ternary operator result type inferring and autounboxing (the JLS should be consulted why it behaves like that)

    But generally, you should try to avoid using wrapper types. Use int instead of Integer. If you need a special value meaning "no result", then you can use Integer.MAX_VALUE for example.

提交回复
热议问题