简体   繁体   English

原始类型的包装器也是原始类型吗?

[英]Are wrappers of a primitive type primitives types too?

I'm confused about data types in Java. 我对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. 我已经在Internet上看到很多图像,它们将Java中的数据类型表示为一棵树,这使我对以前的思考犹豫不决。 An example of those trees are shown as follows: 这些树的示例如下所示: 图1:Java数据类型架构。

So, in another SO post , Buhake Sindi's points that: 因此,在另一篇SO 帖子中 ,Buhake Sindi指出:

Boolean is a wrapper of a primitive type 布尔值是原始类型的包装

Following the previous tree representation of data types in Java, my questions are: 遵循Java中数据类型的先前树表示形式,我的问题是:

  1. A wrapper of a primitive data type is a primitive data type too? 原始数据类型的包装器也是原始数据类型吗? For example Boolean , Integer , Character . 例如BooleanIntegerCharacter
  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; 据我了解, 对象是一个内存区域,可以包含Java中的任何类型。 from primitives to classes created by the programmer. 从原语到程序员创建的类。 So, Object may contain both primitive and non-primitive data types. 因此, 对象可以同时包含原始数据类型和非原始数据类型。 Is that true? 真的吗?

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 ). 这就是它们的要点:当需要对象引用而不是基元(例如List )时,它们用于包装基元。

In that tree graphic, "Boolean" and "Integer" aren't class/type names, they're just labels (like "Floating-point" is). 在该树形图中,“ Boolean”和“ Integer”不是类/类型名称,它们只是标签(就像“ Floating-point”一样)。

Object fits into that tree at the top of "Non-Primitive". Object适合“非原始”顶部的那棵树。

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; 据我了解, Object是一个内存区域,可以包含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. 因此, Object可以同时包含原始数据类型和非原始数据类型。

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 . 不可以,类型为Object (或其任何子类型)的变量,实例成员或参数只能包含对象引用,而不能包含intchar这样的基元。 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 ). 这就是JLS将NumericType拆分为IntegralTypeFloatingPointTyperef )的原因。

And char is an integral type in Java. char是一个整体型 Java编写的。


FWIW, my rough pass at that sketch would look something like this: FWIW,我在该草图上的粗略通行看起来像这样:

在此处输入图片说明

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. :-)请注意,我在各处重复使用“类型”以避免出现使用类名的外观,并且在使用关键字或类名时,我使用字体(就像您的原始字体一样)进行标注。

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, eg, Boolean and Integer are classes, but used here as node labels. “非原始”分支显示类名( StringArray等),而“原始”分支将实际的类名与标签混合,例如BooleanInteger是类,但在此处用作节点标签。 "Floating-point" is not a class, only a label. “浮点数”不是一个类,只是一个标签。

Wrapper classes wrap primitives, eg, the Boolean class wraps the boolean primitive. 包装器类包装基元,例如, Boolean类包装boolean基元。

1. A wrapper of a primitive data type is a primitive data type too? 1. 原始数据类型的包装器也是原始数据类型吗? 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? 2. 对象数据类型在树中应该在哪里? As I understand, Object is a memory region that can contain any type in Java; 据我了解,对象是一个内存区域,可以包含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" . Object是类层次结构的根。每个类都有Object作为超类。所有对象(包括数组)都实现此类的方法” It should be the first element under the non-primitive section of the diagram. 它应该是该图的非基本部分下的第一个元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM