3

I'm confused about data types in Java. I've seen a lot of images on the Internet representing the data types in Java as a tree that makes me hesitate about what I used to think about it. An example of those trees are shown as follows: Figure 1: Java data types schema.

So, in another SO post, Buhake Sindi's points that:

Boolean is a wrapper of a primitive type

Following the previous tree representation of data types in Java, my questions are:

  1. A wrapper of a primitive data type is a primitive data type too? For example Boolean, Integer, Character.
  2. Where should be the Object data type in the tree? As I understand, Object is a memory region that can contain any type in Java; from primitives to classes created by the programmer. So, Object may contain both primitive and non-primitive data types. Is that true?
Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
Tomás Juárez
  • 1,517
  • 3
  • 21
  • 51
  • 1
    ... Wrappers wrap primitives. They're not primitives. Primitives are explicitly not objects; that's why they're called primitives, and why they have wrappers. The object hierarchy provided shows that the primitive wrappers have their own inheritance tree. Noting that not everything under the "Primitive" header is actually a class name--but some are, which makes it a poorly-designed document. – Dave Newton Jan 17 '18 at 15:25
  • 1
    Wrappers are objects, not primitives, but they may get unboxed to primitives as needed . – Arnaud Jan 17 '18 at 15:25
  • 1
    No that diagram is misleading: the leafs are primitives, above are wrappers of primitives - "Numeric" and "Floating-point" are no classes too. – Joop Eggen Jan 17 '18 at 15:25

3 Answers3

5

No, wrappers for primitives aren't primitives. That's the point of them: They're used to wrap primitives when an object reference is required instead of a primitive (such as in a List).

In that tree graphic, "Boolean" and "Integer" aren't class/type names, they're just labels (like "Floating-point" is).

Object fits into that tree at the top of "Non-Primitive".

So for instance, the wrappers would be under non-primitive types:

               Data Type
                 /    \
                /      \
               /        \
              /          \
      Primitive Types    Non-Primitive Types (base class: Object)
            /                         /                  \
           /                         /                    \
    Numeric Types         Primitive Wrapper Types         (etc.)
         /                    /      |      \
        /                    /       |       \
  Integer Types           Char    Integer  Boolean
      /
     /
   char

(Obviously that's very incomplete.)

As I understand, Object is a memory region that can contain any type in Java; from primitives to classes created by the programmer.

No, it's not a memory region. It's a type.

So, Object may contain both primitive and non-primitive data types.

No, a variable, instance member, or parameter of type Object (or any of its subtypes) can only contain an object reference, not a primitive like int or char. That's why we have wrappers for primitives, so we can store them (via a wrapper) where an object reference is expected.


Also note that the diagram is misleading in another way: "Floating Point" shouldn't be under "Integral." In computer science, "integral types" are integers (in mathematics, it's more complex than that). Which is why the JLS splits NumericType into IntegralType and FloatingPointType (ref).

And char is an integral type in Java.


FWIW, my rough pass at that sketch would look something like this:

enter image description here

The final would hopefully be less squashed and ugly. :-) Note that I'm repeating "Types" everywhere to avoid the appearance of using class names, and I've used typeface (like your original did) to call out when I'm using keywords or class names.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • so why can I do something like: int a = 4; Object x = a;? If an Object variable can contain only an object reference that would be throw an error – Tomás Juárez Jan 17 '18 at 15:46
  • 2
    @TomiSebastiánJuárez there is some [Autoboxing](https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html) happening in that case behind the scenes. `a` is _wrapped_ to `Integer` and that's why you don't get any Exception. – Juan Carlos Mendoza Jan 17 '18 at 15:50
1

The graphic is confusing because it mixes up terminology.

The "Non-Primitive" branch shows classnames (String, Array, etc.) while the "Primitive" branch mixes actual classnames with labels, e.g., Boolean and Integer are classes, but used here as node labels. "Floating-point" is not a class, only a label.

Wrapper classes wrap primitives, e.g., the Boolean class wraps the boolean primitive.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
1

1. A wrapper of a primitive data type is a primitive data type too? For example Boolean, Integer, Character.

No. Wrapper of a primitive is not a primitive data type. Are objects which wraps the primitive types. So the diagram is misleading because it places the wrapper classes under the primitive section and should be under the non-primitive section.

2. Where should be the Object data type in the tree? As I understand, Object is a memory region that can contain any type in Java; from primitives to classes created by the programmer. So, Object may contain both primitive and non-primitive data types. Is that true?

Object is not a memory region. "Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class". It should be the first element under the non-primitive section of the diagram.

Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50