4 ms·
> if you wanted to store an integer in a collection, you had to manually convert to and from the primitive int type and the Integer “boxed” class I have never
by JanisErdmanis 1y ago
> if you wanted to store an integer in a collection, you had to manually convert to and from the primitive int type and the Integer “boxed” class
I have never worked with Java. What is this? Why would one want to have a class for an Integer?
- morcus 1y agoIt's because the collection types (and generics) don't support primitives, only objects. So you been to stuff the primitives into objects to use them with a lot of the standard library.
- JanisErdmanis 1y agoThat doesn't sound very pleasant.
- dcminter 1y agoWell, it was annoying until autoboxing came in. // Before autoboxing list.add(new Integer(42)); // After autoboxing list.add(42); Mostly it's a non-issue now. If you're desperately cycle/memory constrained you're likely not using Java anyway.
- marginalia_nu 1y agoYou can get pretty good performance out of Java these days, so long as you know to avoid stuff like boxed primitives and the streams api, as they generally have god-awful memory locality, and generally don't vectorize well.
- dcminter 1y agoYeah, I know there are even oddballs using it for HFT and the like - I like Java a lot, but even I find that a bit peculiar. Edit: actually, if someone here is using it for something like that I'd love to hear the rationale...?
- ivan_gammel 1y agoNot „oddballs“ for sure. Java established itself as the primary enterprise language for fintech in 2000s and since then there was and there is no reason to switch. It offers everything business needs including vast supply of workforce.
- marginalia_nu 1y agoI've worked adjacent to that space (high performance fin-tech java), enough that I feel qualified to answer. It's mostly a trade-off. Java's tooling, reliability and ecosystem is some of the best around. Even though building high performance software in Java is a bit of a pain, looking at the bigger picture, it's often still worth it.
- commandersaki 1y agoIf you're desperately cycle/memory constrained you're likely not using Java anyway. Java Cards would like to have a word with you. But yeah I know what you mean.
- Arwill 1y agoThere are libraries, like fastutil, that provide collections for primitive types.
- coldtea 1y agoEither the same of this feature or always hiding the boxing (e.g. a Python int is actually a wrapper object as well, with some optimizations for some cases like interning) is the case in almost all languages.
- JanisErdmanis 1y agoI personally use Julia, which does not have such boxing issues. Rust, C, C++, and Fortran also avoid boxing like this. Perhaps Go is also free from such boxing? Python does it, that's true.
- dcminter 1y agoOne of the more amusing bugs I had to figure out resulted from the fact that some of the autoboxed values get cached, resulting in peculiar behaviour when someone managed to reflectively change the boxed primitive value... i.e. something like: Integer x = 42 highlyQuestionableCode(x); println(x); // "24" WAT? I'm a fan of JEP-500... https://openjdk.org/jeps/500 https://openjdk.org/jeps/500
- raspasov 1y agoI think your intuition is correct: you probably don’t. That’s also very likely changing. Lookup “project Valhalla”. It’s still a work in progress but the high level goal is to have immutable values that “code like a class, work like an int”. PS When I say “changing”: it’s being added. Java tries hard to maintain backward compatibility for most things (which is great).
- iceboundrock 1y agoPrimitive variables in Java, such as `int`, `boolean`, and `double`, store their actual values directly in memory. When they are local variables inside a method, this memory is typically allocated on the thread's stack. These primitives do not have the structure or overhead of an object, including the object header used by the Garbage Collector (GC) to manage heap-allocated objects. If a primitive value must be treated as an object (e.g., when stored in a Java Collection like ArrayList or when passed to a method that requires an object), Java uses a process called `boxing` to wrap the primitive value into an instance of its corresponding Wrapper class (e.g., Integer, Boolean, Double). These Wrapper objects are allocated on the heap and do possess the necessary object header, making them subject to the GC's management.
- wpollock 1y agoAside from this, having such a class provides a convenient place to hold all the int utility functions, and the same for the other primitive types.
- 1718627440 1y agoCan you have a collection of primitives?
- krior 1y agoNo, but yes. There is nothing in the standard library, that implements the Collection-interface and works with primitives. However you can write your own and there are several implementations of collections for primitives. The problem is that you have to implement them seperately for each type of primitive (e.g. an IntList, DoubleList, BooleanList...).
- 1718627440 1y agoCan't you use templates for that, or do these also only work with objects?
- 1y ago