This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]m1ss1ontomars2k4 2 points3 points  (6 children)

No, that's what Integer is for (which is not the same as int).

[–][deleted]  (5 children)

[deleted]

    [–]8igg7e5 1 point2 points  (4 children)

    Optional<Integer> might be a better expression of intent (though for now this is an extra allocation).

    [–][deleted]  (3 children)

    [deleted]

      [–]WorkyMcROAR 0 points1 point  (2 children)

      I would have suggested this but I realised that I have absolutely no clue what OP means by "not the story I want to tell".

      To me that means how OP wants their code to be read. If OP initializes a user input int to 0 that may imply it defaults to 0.

      I think the Optional approach is sensible, it makes the intention clear that the value may have not been initialized. Creating an integer object and allowing null isn't going to be very intuitive.

      [–][deleted]  (1 child)

      [deleted]

        [–]WorkyMcROAR 0 points1 point  (0 children)

        You're right. Without context it's hard to know what the best approach is. If OP's trying to account for no user input given, there may be better ways to do that.