Tomás Juárez
Tomás Juárez

Reputation: 1514

Are wrappers of a primitive type primitives types too?

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?

Upvotes: 3

Views: 2716

Answers (3)

T.J. Crowder
T.J. Crowder

Reputation: 1074188

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.

Upvotes: 5

Juan Carlos Mendoza
Juan Carlos Mendoza

Reputation: 5794

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.

Upvotes: 1

Dave Newton
Dave Newton

Reputation: 160191

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.

Upvotes: 1

Related Questions