Live data from Hacker News

Rating 26 years of Java changes

neilmadden.blog

11–20 of 327 posts

Re: Rating 26 years of Java changes

#11
post #4

> 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?

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.

That doesn't sound very pleasant.

Re: Rating 26 years of Java changes

#13

> 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?

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

#14
post #4

> 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?

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.

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

#15
post #4

Earlier quoted context omitted.

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.

That doesn't sound very pleasant.

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

#16
-10 for modules is fair, only 4 for lambdas is not. My programming style changed after using lambdas in Java, even when using a different programming language later that doesn't have lambdas as such.

Re: Rating 26 years of Java changes

#17
I think the author is sleeping on Java assertions.

I really like the feature, and it's really one of the features I feel Java got right.

The syntax is very expressive, and they can easily be made to generate meaningful exceptions when they fail.

It's also neat that it gives the language a canonical way of adding invariant checks that can be removed in production but run in tests or during testing or debugging (with -da vs -ea).

You could achieve similar things with if statements, and likely get similar performance characteristics eventually out of C2, but this way it would be harder to distinguish business logic from invariant checking. You'd also likely end up with different authors implementing their own toggles for these pseudo-assertions.

Re: Rating 26 years of Java changes

#18
I haven’t used markdown in javadoc yet but this seems like at least 3/10? I often want to put paragraphs or bulleted lists in javadoc and find myself wanting to use markdown syntax for readability in the code but need to switch to less readable html tags for tooling to render it properly.

Re: Rating 26 years of Java changes

#19
post #4

Earlier quoted context omitted.

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.

That doesn't sound very pleasant.

There are libraries, like fastutil, that provide collections for primitive types.
Post reply on HN