Scalar vs. primitive data type - are they the same thing?

后端 未结 7 568
深忆病人
深忆病人 2020-12-07 07:45

In various articles I have read, there are sometimes references to primitive data types and sometimes there are references to scalars.

My understanding of each is th

7条回答
  •  甜味超标
    2020-12-07 08:15

    I don't think they're interchangeable. They are frequently similar, but the difference does exist, and seems to mainly be in what they are contrasted with and what is relevant in context.

    Scalars are typically contrasted with compounds, such as arrays, maps, sets, structs, etc. A scalar is a "single" value - integer, boolean, perhaps a string - while a compound is made up of multiple scalars (and possibly references to other compounds). "Scalar" is used in contexts where the relevant distinction is between single/simple/atomic values and compound values.

    Primitive types, however, are contrasted with e.g. reference types, and are used when the relevant distinction is "Is this directly a value, or is it a reference to something that contains the real value?", as in Java's primitive types vs. references. I see this as a somewhat lower-level distinction than scalar/compound, but not quite.

    It really depends on context (and frequently what language family is being discussed). To take one, possibly pathological, example: strings. In C, a string is a compound (an array of characters), while in Perl, a string is a scalar. In Java, a string is an object (or reference type). In Python, everything is (conceptually) an object/reference type, including strings (and numbers).

提交回复
热议问题