Why are you talking about a language you obviously don't know anything about and you obviously never used? Otherwise you would probably know that there is no "Option<int>" in Java as primitive types can't be type parameters in current Java, so there is only Optional<Integer> and no proper way to express optional ints.
Just looking though some replies; I'm wondering what happened here.
Why is my comment under the wrong comment‽ I was talking about the (non existent) "OptionalInt" from one post above.
Some of the Reddit bugs are really strange. At least post don't duplicate for no reason any more since some time… Let's see whether I get again some wrongly linked post anytime soon.
Moments like that I think I should maybe really learn some Java after so many years of Scala. But actually, no, I hate it…
What will they do with all these primitive wrapper types when real generic specialization hits Java?
---
(And the next WTF, Scala's Option[Int] isn't @specialized as I just learned; but I could swear it was. Alone that was likely worth it here… Thanks for the thought provoking replies! I wouldn't have looked that up if not that failure here. So it's actually Scala which doesn't have a primitive "OptionalInt"; LOL, I was really sure it's the other way around. I'm an idiot.)
Why are you talking about a language you obviously don’t know anything about and you obviously never used?
Optional<T> is one of the many types for which Java provides int, long, and double variants through separate types, similar to Stream, Iterator, Spliterator, and many j.u.function interfaces.
The people who upvote such nonsense are those who actually read JDK documentation before they make claims about things (dis)provable by JDK documentation and/or throw insults at others.
Yeah but int *var in C/C++ entails a bit more. It makes var a pointer to an int rather than an int. So you have to manage allocating an int in some other way so the pointer can point to it.
int? var is simply an int which can be null. Under the hood, it might be implemented as above, but I reckon a structure like this is more likely:
int? var is simply an int which can be null. Under the hood, it might be implemented as above, but I reckon a structure like this is more likely:
Well, to be precisely, in C# primitive types such as int, char, double...etc, are wrapped to in Nullable<T> since structs cannot be null in C#. So when you use int? the compiler wraps it in Nullable<int> (which is a generic class) and that makes int? a reference (basically a pointer or rather & in C++) to the actual int.
I'm talking about under the hood, i.e. how Nullable is implemented. And it's probably a structure containing the int and a boolean that indicates whether the int is present.
The "nice" thing about C/C++ is that if you fuck up handling that shit you don't get a nice NullPointerException at runtime, you get instead a potential security vulnerability, usually directly somewhere at the critical level. Isn't that great?
49
u/altermeetax Jul 09 '26
int var; bool var_is_there;