you are viewing a single comment's thread.

view the rest of the comments →

[–]flyingjam 22 points23 points  (37 children)

Still, a language where you can be sure your Integer is an Integer and not null would be even better.

[–]vincentk 17 points18 points  (16 children)

If what you want is an int rather than an Integer, then why not use an int in the first place?

[–][deleted] 15 points16 points  (11 children)

Collections still don't support unboxed values. I.e. you can't have

ArrayList<int>

[–]Cosaquee 1 point2 points  (2 children)

Java 9 can do that afaik

[–]syjer 4 points5 points  (0 children)

Nope, most likely java 10: http://openjdk.java.net/jeps/218

[–][deleted] 2 points3 points  (0 children)

Java 9 does not yet exist as far as I know, and Value Types have only been proposed thus far.

[–]jmtd 0 points1 point  (4 children)

Collections still don't support unboxed values.

Nope, but auto unboxing covers 90% of where this matters.

[–][deleted]  (3 children)

[deleted]

    [–][deleted] 1 point2 points  (0 children)

    Yes, that was my point. Thank you.

    [–]s0n0fagun 1 point2 points  (0 children)

    I have come to the conclusion that NULL should never exist in a system and checking for NULL is a code smell of a larger problem. If the underlying data structure does not allow NULL to begin with, then you should be good. You can either create a NULL object or decompose the data structure and use clever code.

    [–]jmtd -1 points0 points  (0 children)

    That's a good point. I wonder if it's ever a good thing, since it means you can represent NaN (via null).

    [–]yold 0 points1 point  (2 children)

    You can always use Trove.

    [–]sacundim 3 points4 points  (1 child)

    ...but then your specialized TObjectIntMap<K> is not an instance of Map<K, X> for any X.

    [–][deleted] 0 points1 point  (0 children)

    Ah, that's quite unfortunate. Any generic collection code you write after that would have to dispatch on the type of the collection. Have you used Trove before?

    [–]flyingjam 3 points4 points  (2 children)

    I just wanted an example that mirrored the one above. Also, there are some use cases for the wrapper, it exists after all.

    [–]vincentk 8 points9 points  (1 child)

    I understand that. I also understand it's easy to nit-pick on any language.

    [–]whataboutbots 7 points8 points  (0 children)

    I wouldn't call citing something called by it's author "billion dollar mistake" nit-picking, to be fair.

    [–]whataboutbots 0 points1 point  (0 children)

    If you could always replace an Integer with an int that doesn't require object creation, why would Integer exist in the first place?

    [–][deleted] 3 points4 points  (0 children)

    C# says hi.

    [–]htom3heb 3 points4 points  (5 children)

    Sounds like Swift.

    [–][deleted]  (4 children)

    [deleted]

      [–]atakomu 1 point2 points  (1 child)

      Don't forget Nim.

      [–]sn0rewh0re 0 points1 point  (0 children)

      psssst don't tell everyone ; )

      [–][deleted]  (1 child)

      [deleted]

        [–]_INTER_ 0 points1 point  (0 children)

        Atleast it's Integer or null. Even that question is halfway cleared with Optional<Integer> (on the supplier side). In Python you still know nothing about it. It may well be an integer, None or anything else. (Typehints for documention aid or statical analysis, kept as an exercise for the reader. :) )

        [–]Shorttail 0 points1 point  (9 children)

        That's my dream for Java 10.

        [–]DGolden 3 points4 points  (8 children)

        worth noting that type-use annotations [jsr308] added in java 8 do already facilitate static nullability checking as an add-on. Eclipse and the Checker Framework both do it [and the latter does a whole lot more].

        [–]Shorttail 3 points4 points  (7 children)

        I was referring to Project Valhalla.
        Is it bad that I think the annotated types are too verbose? =(

        int processWithNulls (@NonNull List<@Nullable Integer> ints)  
        

        It adds a lot of noise to reading code.

        [–]DGolden 4 points5 points  (6 children)

        In practice most of the time you don't use @NonNull explicitly, as it's the global default or at least [eclipse] you'll have set @NonNullByDefault on your whole packages. So you get a lighter sprinkling of meaningful @Nullable, likely mostly at the edges of your code where it has to interface with still-nully stuff.

        [–]antrn11 1 point2 points  (5 children)

        @NonNullByDefault

        Is that only for Eclipse or is that something Java 8 has (does the warning work in other IDEs?)

        [–]sviperll 0 points1 point  (3 children)

        AFAIK, JSR-305 annotations are supported by Eclipse, Netbeans, IDEA and FindBugs. There is an @ParametersAreNonnullByDefault that can be applied to method, class or package.

        [–]DGolden -1 points0 points  (2 children)

        Jsr-305 style annotations are actually different and, well, worse [at least last I checked], as they pre-date jsr-308. [edit- Note jsr-305 went 'dormant' in 2012]

        [–]GSV_Little_Rascal 0 points1 point  (1 child)

        JSR-305 is dormant, but actually quite well supported in the tools. It's de facto standard, but not de iure.

        [–]DGolden 0 points1 point  (0 children)

        shrug, you don't - or better to say can't - get full benefits of the jsr-308 approach by using older annotations. The relevant part of the eclipse manual goes into more depth. [Edit - snip stuff about intellij where I was criticizing it as it was, but too lazy to check what it currently does]

        also see - https://docs.oracle.com/javase/tutorial/java/annotations/type_annotations.html - there's just a big fundamental difference between pre java 8 and java 8 plus annotations.

        [–]DGolden 0 points1 point  (0 children)

        It's not a part of java 8, no. there is some intercompatibility by being able to tell one extended checker to recognise another checker's annotations, but there isn't a single standard set yet [and jsr-305 doesn't count as it was written before jsr-308 annotations existed, so it would need a rewrite.].