标量与原始数据类型

编程入门 行业动态 更新时间:2024-10-26 02:30:56
标量与原始数据类型 - 它们是一样的吗?(Scalar vs. primitive data type - are they the same thing?)

在我阅读的各种文章中,有时引用了原始数据类型,有时还引用了标量。

我对每个人的理解是,它们是简单的数据类型,如int,boolean,char等。

有缺少的东西,这意味着你应该使用特定的术语,或者这些术语是否可以互换? 维基百科的页面不显示任何明显的。

如果这些术语是可以互换的,哪个是首选的?

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 that they are data types of something simple like an int, boolean, char, etc.

Is there something I am missing that means you should use particular terminology or are the terms simply interchangeable? The Wikipedia pages for each one doesn't show anything obvious.

If the terms are simply interchangeable, which is the preferred one?

最满意答案

我不认为他们是可以互换的。 他们经常是相似的,但差异确实存在,似乎主要在于它们与上下文的对比和相关。

标量通常与化合物 (例如数组,映射,集合,结构等)形成对比。标量是“单个”值 - 整数,布尔值,也许是字符串 - 而复合物由多个标量组成(可能还涉及到其他化合物)。 “Scalar”用于在单/简单/原子值和复合值之间的相关区别的上下文中。

然而, 原始类型与例如引用类型形成对比,当相关的区别是“这直接是一个值,还是它是包含真实值的东西的引用”时使用,如Java的原始类型与参考。 我认为这是一个比标量/复合稍低一级的区别,但不完全相同。

它真的取决于上下文(通常是什么语言家庭正在讨论)。 拿一个,可能病理的例子:字符串。 在C中,一个字符串是一个复合(一个字符数组),而在Perl中,一个字符串是一个标量。 在Java中,字符串是一个对象(或引用类型)。 在Python中,一切都是(概念上)一个对象/引用类型,包括字符串(和数字)。

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).

更多推荐

本文发布于:2023-07-24 01:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1240162.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标量   数据类型   原始

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!