简体   繁体   English

Java和C#中“primitive”,“value type”,“struct”,“class”,“wrap”的定义

[英]Definitions of “primitive”, “value type”, “struct”, “class”, “wrap” in Java and C#

I have been trying to understand the use of "primitives" in Java and C# and the difference between them (if any). 我一直试图理解在Java和C#中使用“原语”以及它们之间的区别(如果有的话)。 I have asked a series of questions on SO and some of the answers seem to confuse the issue rather than clarify it. 我已经提出了一系列关于SO的问题,一些答案似乎混淆了问题而不是澄清它。 Some answers (and some MS documentation) appear to provide contradictory statements. 一些答案(和一些MS文档)似乎提供了相互矛盾的陈述。 From SO 来自SO

and from MS: http://msdn.microsoft.com/en-us/library/ms228360%28VS.80,lightweight%29.aspx - "structs are very similar to classes" - "the Int32 class wraps the int data type" - "On the other hand, all primitive data types in C# are objects in the System namespace. For each data type, a short name, or alias, is provided. For instance, int is the short name for System.Int32". 并从MS: http//msdn.microsoft.com/en-us/library/ms228360%28VS.80,lightweight%29.aspx - “结构与类非常相似” - “ Int32类包装int数据类型“ - ”另一方面,C#中的所有原始数据类型都是System命名空间中的对象。对于每种数据类型,都提供了一个简短的名称或别名。例如, int是System.Int32的简称“。

My confusion lies largely with C# (I have programmed java for some while). 我的困惑很大程度上与C#(我已经编程java一段时间)有关。

EDIT The following paragraph has been confirmed to be correct by @Jon Skeet 编辑以下段落已被@Jon Skeet确认为正确

  • Java has two types (primitive and class). Java有两种类型(原始类和类)。 The words "value type" could be a synonym for primitive (although not widely used) and "reference type" for class. 单词“value type”可以是基元的同义词(虽然没有广泛使用)和类的“引用类型”。 Java "wraps" primitives (int) in classes (Integer) and these classes have the complete power of any other class (can be null, used in collections, etc.) Java在类(整数)中“包装”原语(int),这些类具有任何其他类的完整功能(可以为null,在集合中使用等)

EDIT @Jon has given a very clear statement on C# so I will delete my suggested truths and refer to his answer. 编辑 @Jon已经对C#做了非常明确的陈述,所以我将删除我建议的真相并参考他的答案。

Further question: Is there a consensus on what the actual use of these terms should be? 进一步的问题:对这些术语的实际用途应该有什么共识? If there is a consensus I'd be very grateful for it spelt out explicitly. 如果达成共识,我将非常感谢它明确阐述。 Otherwise I assume the terminology is muddled and therefore of limited use. 否则我认为术语混乱,因此使用有限。

SUMMARY Thanks for very clear answers. 总结感谢您非常明确的答案。 There is a consensus (see accepted answer from @Jon) among those who really understand this and the MS docs are consistent (although they refer to Java in places and I misread these for C#) 在那些真正了解这一点并且MS文档是一致的人之间存在共识(参见@Jon接受的答案)(尽管他们在某些地方引用了Java而我误解了这些用于C#)

I haven't seen MS docs to be contradictory about this (MSDN is sometimes wrong, but on this specific issue, I've always seen it correct). 我没有看到MS文档与此相矛盾(MSDN有时是错误的,但在这个具体问题上,我总是看到它是正确的)。 The MSDN link you posted says: 您发布的MSDN链接说:

For each primitive data type in Java , the core class library provides a wrapper class that represents it as a Java object. 对于Java中的每个原始数据类型 ,核心类库提供了一个包装类,将其表示为Java对象。 For example, the Int32 class wraps the int data type, and the Double class wraps the double data type. 例如,Int32类包装int数据类型,Double类包装double数据类型。

On the other hand , all primitive data types in C# are objects in the System namespace. 另一方面 ,C#中的所有原始数据类型都是System命名空间中的对象。 For each data type, a short name, or alias, is provided. 对于每种数据类型,都提供了短名称或别名。 For instance, int is the short name for System.Int32 and double is the short form of System.Double. 例如,int是System.Int32的短名称,double是System.Double的缩写形式。

Basically, it's saying the right thing. 基本上,它说的是正确的。 In Java, Integer class is a wrapper for int primitive type. 在Java中, Integer类是int基本类型的包装器。 In C#, int is an alias for System.Int32 structure. 在C#中, intSystem.Int32结构的别名。 The first paragraph is about Java and doesn't apply to C#. 第一段是关于Java的, 不适用于C#。


In .NET, the terminology is as follows: 在.NET中,术语如下:

A primitive type is a type that has IsPrimitive property set to true. 基本类型是将IsPrimitive属性设置为true的类型。 Primitive types are: 原始类型是:

The primitive types are Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double, and Single. 基元类型是布尔,字节,SByte,Int16,UInt16,Int32,UInt32,Int64,UInt64,IntPtr,UIntPtr,Char,Double和Single。

All primitive types are value types but not vice versa . 所有原始类型都是值类型,但反之亦然

A value type is a type that has value semantics, as opposed to reference semantics . 值类型具有值语义类型,而不是引用语义 The whole value is copied when passed by value (not its reference). 通过值(而不是其引用)传递时复制整个值。 Local variables of value types are stored on stack. 值类型的局部变量存储在堆栈中。 struct s and enum s are value types. struct s和enum是值类型。

As mentioned above, all primitive types are value types. 如上所述,所有原始类型都是值类型。 They are struct s in the System namespace. 它们是System命名空间中的struct In C#, int , double , etc., keywords are basically aliases for those struct s. 在C#, intdouble等中,关键字基本上是这些struct的别名。

The first bullet point is correct. 第一个要点是正确的。

The second is not: the primitive types in .NET are Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double, and Single. 第二个不是:.NET中的原始类型是Boolean,Byte,SByte,Int16,UInt16,Int32,UInt32,Int64,UInt64,IntPtr,UIntPtr,Char,Double和Single。 A struct cannot usually be set to null , but there are also nullable value types ( Nullable<T> ). 结构通常不能设置为null ,但也有可为空的值类型( Nullable<T> )。 These are still value types, but there's syntactic sugar in C# to equate "null" with "the null value for the type", which for a nullable value type is an instance where HasValue returns false. 这些仍然是值类型,但C#中的语法糖将“null”等同于“类型的空值”,对于可空值类型, HasValue返回false的实例。

int and System.Int32 are exact synonyms in C#. intSystem.Int32 C# 中的完全同义词。 The former is just an alias for the latter. 前者只是后者的别名。 They compile to exactly the same code. 他们编译成完全相同的代码。

In C#, classes and interfaces are always reference types. 在C#中,类和接口始终是引用类型。 Structs and enums are value types - but there is a boxed equivalent of every struct (other than Nullable<T> which is handled differently by the CLR in terms of boxing). 结构和枚举是值类型 - 但是每个结构都有一个盒装的等价物( Nullable<T>除外,CLR在装箱方面有不同的处理方式)。 The boxed types don't have separate names, and can't be referred to explicitly in C# (although they can be in C++/CLI). 盒装类型没有单独的名称,并且不能在C#中显式引用(尽管它们可以在C ++ / CLI中)。 There is no separate wrapper class equivalent like java.lang.Integer in .NET; 没有单独的包装类,如.NET中的java.lang.Integer ; it would be problematic to introduce such classes as you can create your own value types in .NET, unlike in Java. 引入这样的类是有问题的,因为您可以在.NET中创建自己的值类型,这与Java不同。

For more information on reference types and value types, see my article about it . 有关引用类型和值类型的更多信息,请参阅我的文章

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

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