简体   繁体   English

基本类型和原子类型有什么区别?

[英]What is the difference between a primitive type and an atomic type?

Many programming language specifications talk about the language's primitive types and their atomic types. 许多编程语言规范都谈论该语言的原始类型及其原子类型。 In some, these are mentioned separately, and some mix the definitions together. 在某些情况下,将分别提及这些内容,而有些则将这些定义混合在一起。 After reading some material I have come to realize that there is a difference between the two terms, however it seems to be either very subtle or too obscure. 在阅读了一些材料之后,我开始意识到这两个术语之间存在差异,但是看起来要么很微妙,要么太晦涩。

What exactly is the difference between an atomic type and a primitive type ? 原子类型原始类型之间到底有什么区别? Is one contained within the other? 一个包含在另一个中吗?

The difference is that primitive types ( int etc) are not objects; 不同之处在于原始类型( int等)不是对象; although in Java there are classes associated to each primitive type ( Integer for int etc). 尽管在Java中,有一些与每种基本类型相关联的类(用于int Integer等)。

Atomic types are "just" regular Java objects; 原子类型是“只是”常规Java对象; the difference with a "plain" object is that the methods to manipulate them are guaranteed to be atomic, therefore thread safe. 与“普通”对象的区别在于,操作它们的方法被保证是原子的,因此是线程安全的。 For int , that would be AtomicInteger , which you can increment, .getAndSet() etc atomically. 对于int ,它将是AtomicInteger ,您可以原子地递增.getAndSet()等。

It is partially incorrect to talk about "atomic types" vs "primitive types" however, since some classes which support atomic operations are not linked to primitive types; 但是,谈论“原子类型”与“原始类型”在某种程度上是不正确的,因为某些支持原子操作的类未链接到原始类型。 one such example is AtomicReference , but you also have AtomicIntegerArray and a few others. 一个这样的示例是AtomicReference ,但是您也有AtomicIntegerArray和其他一些示例。

See also the Wikipedia entry on compare and swap , or CAS for short. 另请参阅compare和swap上的Wikipedia条目,或简称CAS。


Also, while one would think that primitive types are fundamentally thread safe, this is not the case; 同样,虽然人们会认为基本类型从根本上讲是线程安全的,但事实并非如此。 each thread can have its own local storage (this is referred to, uninspiringly, as TLS, Thread Local Storage) for optimization, and can therefore have its own copy of primitive variables; 每个线程可以具有自己的本地存储(这毫无疑问地称为TLS,线程本地存储)以进行优化,因此可以具有自己的原始变量副本; this is why in Java you have volatile . 这就是为什么在Java中您具有volatile

Primitive types are atomic. 基本类型是原子的。 In other words, they cannot be de-constructed into simpler types. 换句话说,它们不能被解构为更简单的类型。

In Java there are Primitive types and there are boxed types, Box types are actual objects and primitive types are just variables of a type. 在Java中,有原始类型和装箱类型,Box类型是实际对象,而原始类型只是类型的变量。

For example int is a primitive type, while Integer is a boxed type. 例如,int是原始类型,而Integer是盒装类型。

int pi ; int pi; Integer bi; 整数bi;

pi is declared AND allocated and can be used immediately, eg: pi = 100 ; pi被声明并分配,并且可以立即使用,例如:pi = 100;

bi is declared, but it is NOT allocated until: bi = new Integer(1); 声明了bi,但直到以下时间才分配它:bi = new Integer(1);

Before allocation referencing bi will throw a java.lang.NullPointer error. 在分配之前,引用bi将引发java.lang.NullPointer错误。

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

相关问题 原始类和原始数据类型之间有什么区别? - What is the difference between a primitive class and primitive data type? Java中的long对象和基本类型long之间有什么区别? - What is difference between Long object and primitive type long in Java? 基本类型和包装器类之间的主要区别是什么? - What is the main difference between primitive type and wrapper class? 原语和包装器数据类型的使用有什么不同以及包装器数据类型的需求是什么? - What is the difference in usage of primitive and wrapper data type and what is the need of wrapper data type? 有界通配符和类型参数之间有什么区别? - What is the difference between bounded wildcard and type parameters? Scala(和Java)中的类和类型之间有什么区别? - What is the difference between a class and a type in Scala (and Java)? 通配符和类型变量之间有什么区别? - What is the difference between wildcards and type variables? Primitive array和Reference Array有什么区别? - What is the difference Between Primitive array and Array of Reference . 原始类型和引用类型有什么区别? - What's the difference between primitive and reference types? 原子/易失性/同步之间有什么区别? - What is the difference between atomic / volatile / synchronized?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM