Home > java > Java Value Types and Generics specialization

Java Value Types and Generics specialization

It is exciting to see that Brian Goetz and others are working towards Generics over Primitive Types (List<int> et al) and Value Types (structured immutable data types with value semantics and without object overhead) for Java 10.

The motivation for these initiatives comes from the somewhat clunky divide between objects and primitives in Java.

Objects are the only choice in Java when it comes to collections content and generic type instantiation in general. There is no such thing as List<int> in Java right now.

Objects are also the only option for structured data in Java, including simple aggregation. There are no structs, no tuples, etc. And because the primitive types in Java cannot be “primitively” extended, combined or range restricted, even things like complex numbers, unsigned ints, big decimals and the like all require classes and objects to represent the values.

Arrays are the only way to aggregate a bunch of primitives (of the same type) in Java. But Arrays themselves are objects, with reference identity not value identity, and cannot be used as a substitutes for tuples.

Object purists might argue that Java should just get rid of primitives altogether and strictly follow the “everything is an object” paradigm.

But objects bear management overheads in terms of memory and processing time, bring complexity around identity versus equality, require reference checking for garbage collection purposes, cannot live on the stack, lead to array data in non-contiguous memory, etc.

As the Java architects point out, there currently is an unsolvable conflict between object identity and value semantics in Java: Even if objects a1 and a2 of immutable type A have completely identical state, the JVM cannot treat them as identical “values”, because synchronization relies on monitor object identity, and A1 and A2 would represent separate independent locks. Conflating them into one “value object” would break that distinction.

All this suggests that Java needs to be extended to support structured types with value semantics, conceptually somewhere between primitives and objects. Hopefully Java 10 will come with such types and hopefully smart engineers like Brian Goetz will make sure the resulting type system won’t feel like an incoherent Frankenstein monster.

Advertisements
Categories: java Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: