10

Is there a particular reason behind wrapper classes (java.lang.Integer, java.lang.Boolean, ...) not having a common supertype ?


I'm asking because it would be quite handy to have (e.g.) WrapperType::getType function along the classic Object::getClass which would return the class of the primitive type.

More specifically, the context is invoking constructors via reflection where you only have the Class<T> and the parameters Object[]

E.g:

public static <T> T createInstance(Class<T> clz, Object... params) throws Exception

In order to get the constructor I can get the parameter types via:

Class<?>[] c = Arrays
        .stream(params)
        .map(Object::getClass)
        .toArray(Class<?>[]::new);
return clz.getConstructor(c).newInstance(params);

but this will of course fail with constructors like String::new(char[], int, int);

If that supertype existed I could do:

.map( o -> o.getClass().isPrimitive() ? ((WrapperType) o).getType() : o.getClass() )

I guess there is a particular reason java developers did not implement it.

10
  • 3
    BTW, in Scala, there is a common supertype for wrapper classes - AnyVal. Apr 26, 2018 at 13:28
  • 1
    Idk if you know this but Number is the super class for all wrapper classes expect String, Character, and Boolean. There's really no shared functionality between Numbers, String, Character, and Boolean other than toString() so there's no point in making a naming type as a super for the wrappers ti share.
    – Thatalent
    Apr 26, 2018 at 13:38
  • 6
    Wrappers were around before generics, so a generic supertype wasn't really an option.
    – khelwood
    Apr 26, 2018 at 13:42
  • 2
    I'm struggling to find multiple use cases for something like this. Even though wrappers were implemented before generics, I don't see any reason why they couldn't have added it in later. The only reason I can see is that it wasn't worth the resources, and probably still isn't. If you can find multiple common use cases for this, then it could probably be requested. But seeing how these types deeply impact many applications, it sounds like it would be somewhat expensive to analyze potential issues, to the point of outweighing the benefits.
    – Vince
    Apr 26, 2018 at 13:51
  • 3
    Calling getConstructor like that doesn't work anyway, because some methods accept boxed types (e.g. compareTo) and sometimes the arguments to a method are subtypes of the parameter types. (Also, o.getClass().isPrimitive() will always be false.) To do this correctly, you need to do applicability testing by testing every constructor or just know the parameter types ahead of time.
    – Radiodef
    Apr 26, 2018 at 13:55

2 Answers 2

1

Java designers probably aren't too abstract in their decisions and couldn't find many similarities between numeric types (e.g. Integer) and non-numeric types (e.g. Boolean) to group them by a superclass.

Though, all the numeric types extend Number which truly represents "numeric values that are convertible to the primitive types byte, double, float, int,long, short".

Actually, all the wrapper classes have a common interface Comparable<T> (e.g. Comparable<Integer> for Integer).

1
  • the point is that java.lang.Number doesn't provide a (e,g.) getPrimitiveClass method either
    – payloc91
    Apr 27, 2018 at 8:49
1

I can see how practical this would be, but in terms of abstraction this would be a big mess. Let's say you have the wrappers Integer and Double along with the others in that family, they do have java.lang.Number as their super. However there is no semantic relationship between them and a Boolean ( taking in c influences apart).

But for argument sake, let's do consider they are all in a big bucket and are Wrappers, are they still Numbers?

Considering the languages historic aversion to multiple inheritance, having Numbers would make much more scene if you were to pick.

After the big revolutionary change in the language capabilities with SE 8, we are now able to have multiple inheritance through interfaces while using standard implementations for such cases. And if one analyse further, they will within a logical reasoning conclude that those cases call for contracts and not for inheritance; thus, even if done, this would be a job for interfaces.

Taking in account that a solid way to implement this is quite new, and that Wrappers would be a over generalized way to do it, we can see that is better not to have it. Although it is now possible, there are historical and semantic reasons for why it was not presente, at least as a super class. Abstraction is key here. But nothing keeps you from inserting a factory in you pipeline, or implement the mentioned interface.

Buttom line, the key factors are:

  1. Abstraction: this is a more like contract or a is-a relationship
  2. History: generics, and multiple inheritance through interfaces
  3. Imagine the size of the inheritance three if we would accommodate cases like: it is a wrapper first, then a Number or a Boolean..
4
  • There have been many changes in existing classes from java 7 -> 8. I don't see why a common interface PrimitiveWrapper with a method getType() would make a big mess. Also (as far as my question intended) Number does not expose a getType() method either. The key of my question was how to easily get the primitive type of a wrapper class. Thx for the insight though
    – payloc91
    Apr 26, 2018 at 14:27
  • 1
    I was actually saying that you could have an interface. Sorry if not clear. A super class would be messy, and this is a contract that could be done in an interface.
    – Victor
    Apr 26, 2018 at 14:35
  • Also, there is the historic factor as well, so a better question is not why it did not exist, but why not now.
    – Victor
    Apr 26, 2018 at 14:40
  • The question focuses on a common supertype - I don't believe he was talking about classes specifically. How the question was phrased, I don't believe he cares about the history (why it did not exist), rather why they currently don't share a common interface.
    – Vince
    Apr 26, 2018 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.