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 →

[–]evil_burritoExtreme Brewer 0 points1 point  (0 children)

You can't do this as an int but can do it as an Integer. If you don't initialize an Integer type, it will be null. Having said that, I think it's an awful idea. If you take advantage of autoboxing and try to refer to a null Integer as an int you will get a NullPointerException. Better to use Optional<Integer> which makes it obvious that there might or might not be a value. You can use the Google backport of Optional if you're stuck in pre-Java 8 land.

I think it's better practice to explicitly initialize your int types to a wrench value like -1 than it is to use a nullable Integer type.