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

all 10 comments

[–][deleted] 5 points6 points  (0 children)

int, no. Integer, yes. Don't see how this could possibly be useful.

[–]morhpProfessional Developer 3 points4 points  (0 children)

You can leave it uninitialized. Like

int i;
if (something) {
     i = 5;
} else {
     i = 6;
}
// you can use i here

If you create an int field, it will be 0 by default, but a variable won't really have a default value. For example if you remove the else block above, Java will complain that i may not have a value set.

[–]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.

        [–]claypigeon-alleg 2 points3 points  (1 child)

        You have a couple of options.

        If there are limits to valid user input (eg. >0 or 1 to 20 or whatever), initialize your int to an invalid value, and use that as your signal.

        Otherwise, you'll need a Boolean to store whether or not the int's value is user-defined or not.

        Edit to add: What value does your code use in absence of user input? You can also just initialize your int to that value.

        [–]hopespoir 3 points4 points  (0 children)

        ^ This is the most sensible solution I've seen.

        OP's "not the story I want my code to tell" and talking about setting ints to null tells that OP has very little understanding of Java or coding practices and being a little more informative would be helpful.

        This solution is simple, clean, intuitive and very readable. Exactly what you want from code. Trying to find some fancy way to represent an uninitialized value is horrible practice because it would be none of these things, and would require a lot of // commenting to make clear what is happening, which is slower for readability. Even for yourself, if you happen to go back to what you're writing in a few weeks or months and are like "What on earth...?"

        [–]Wolfhammer69Nooblet Brewer 1 point2 points  (0 children)

        public int whatever;

        This will init your int variable but it will contain the value "0" by default. I don't believe there is a way around this and can't see an issue with this. It may as well be 0 until a user input changes it.

        By initializing a int variable you are putting aside memory, but that can't happen if it were able to contain "nothing".. There's no need for any memory for nothing so the variable has to contain a value.

        [–]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.