Conversion from null to int possible?

前端 未结 6 1933
刺人心
刺人心 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条回答
  •  萌比男神i
    2020-12-08 02:20

    Guava has a pretty elegant solution for this using MoreObjects.firstNonNull:

    Integer someNullInt = null;
    int myInt = MoreObjects.firstNonNull(someNullInt, 0);
    

提交回复
热议问题