News for hackers. Stuff that matters.
Re:Rating 26 years of Java changes by JanisErdmanis on Saturday October 11, 2025 @04:07PM (#45552256)
> 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?

Re:Rating 26 years of Java changes by morcus on Saturday October 11, 2025 @04:11PM (#45552275)
It'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.
Re:Rating 26 years of Java changes by JanisErdmanis on Saturday October 11, 2025 @04:21PM (#45552362)
That doesn't sound very pleasant.
Re:Rating 26 years of Java changes by dcminter on Saturday October 11, 2025 @04:30PM (#45552430)
Well, 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.
Re:Rating 26 years of Java changes by marginalia_nu on Saturday October 11, 2025 @04:44PM (#45552525)
You 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.
Re:Rating 26 years of Java changes by dcminter on Saturday October 11, 2025 @04:50PM (#45552572)
Yeah, 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...?

Re:Rating 26 years of Java changes by ivan_gammel on Sunday October 12, 2025 @01:51AM (#45555622)
Not „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.
Re:Rating 26 years of Java changes by marginalia_nu on Sunday October 12, 2025 @06:50AM (#45557169)
I'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.

Re:Rating 26 years of Java changes by commandersaki on Sunday October 12, 2025 @09:20AM (#45558057)
If 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.

Re:Rating 26 years of Java changes by Arwill on Saturday October 11, 2025 @04:36PM (#45552470)
There are libraries, like fastutil, that provide collections for primitive types.
Re:Rating 26 years of Java changes by coldtea on Saturday October 11, 2025 @04:41PM (#45552508)
Either 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.
Re:Rating 26 years of Java changes by JanisErdmanis on Saturday October 11, 2025 @05:25PM (#45552845)
I 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.
Re:Rating 26 years of Java changes by dcminter on Saturday October 11, 2025 @04:25PM (#45552399)
One 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

Re:Rating 26 years of Java changes by raspasov on Saturday October 11, 2025 @04:20PM (#45552347)
I 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).

Re:Rating 26 years of Java changes by iceboundrock on Saturday October 11, 2025 @04:22PM (#45552370)
Primitive 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.

Re:Rating 26 years of Java changes by wpollock on Saturday October 11, 2025 @05:41PM (#45552950)
Aside 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.
Re:Rating 26 years of Java changes by 1718627440 on Friday October 17, 2025 @06:49PM (#45623038)
Can you have a collection of primitives?
Re:Rating 26 years of Java changes by krior on Saturday October 18, 2025 @07:24PM (#45631090)
No, 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...).
Re:Rating 26 years of Java changes by 1718627440 on Sunday October 19, 2025 @10:54AM (#45634611)
Can't you use templates for that, or do these also only work with objects?
Re:Rating 26 years of Java changes by krior on Tuesday October 21, 2025 @09:07AM (#45655289)
Templates do not exist in Java.
Re:Rating 26 years of Java changes by 1718627440 on Tuesday October 21, 2025 @05:58PM (#45662251)
My bad, I think they are called Generics in Java. I always thought of these as the same thing, just with different names in different languages, is that wrong?
Re:Rating 26 years of Java changes by krior on Friday October 24, 2025 @04:54AM (#45692439)
Templates and generics are very different. Afaik Templates generate specialised code for each invocation of that template with a different type.

Java uses Type Erasure for Generics, which means that generic type variables are checked at compile time, but that type information is not available at runtime. At compile time you may have List<Foo> and List <Bar> but at runtime its all just List<Object> (Object being the base type everything inherits from). Basically List stores a bunch of pointers to objects and since pointers can point to anything a List can basically "store" anything.

Then you have a divide between primitives and Objects in Java, where primitives are double, int, boolean etc.. Primitives are types in the C-sense, e.g. int is nothing more than a 32-bit number, not a class in the Java-sense. But since ints are not classes and as such do not inherit from Object, they cannot be placed inside of List<Object>. Therefore there cannot be a List<int>. But there is Integer, which is an Object-wrapper around an int, meaning you can have a List<Integer>. This is called boxing and like all indirection carries a performance penalty.

Java hopes to heal this rift between primitives and Objects through something called Project Valhalla which has been 10 years in the making and is expected to land in the next two years or so.

Hope this ramble is comprehensible.

Re:Rating 26 years of Java changes by 1718627440 on Saturday October 25, 2025 @07:56PM (#45707888)
Thanks it is. So the compiler would bark on List<int> ?
Re:Rating 26 years of Java changes by krior on Sunday October 26, 2025 @03:30PM (#45714554)
Yes, exactly.