什么是统一型系统? [英] What is a unified type system?

查看:97
本文介绍了什么是统一型系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读 C#和Java的比较,清单上的第一件事就是单根(统一)类型系统。

I've read a comparison of C# and Java and the first thing on the list is "Single-root (unified) type system".

您能描述单根(统一)类型系统的含义吗?

Can you describe what a single-root (unified) type system means?

推荐答案

C#有一个统一的类型系统。 所有C#类型,包括基本类型,如int和double,从单个根对象类型继承。与类对象不同,这些基本类型是值类型。它们不是单独堆分配的,而是按值传递的。

C# has a unified type system. All C# types, including primitive types such as int and double, inherit from a single root object type. Unlike class objects, these primitive types are value-types. They are not separately heap-allocated, and they are passed by value.

当一个C#值类型(例如一个原始int或用户定义的struct)被放置在一个参数集合中时,它被存储在一个密集的数组中,没有指针。这是可能的,因为C#为每个不同的参数大小进行自定义参数实例化。这意味着当您实例化C# List< int> 时,基础数组列表存储密集的int数组。

When a C# value type (such as a primitive int, or user-defined struct) is placed in a parametric collection, it is stored in a densely packed array with no pointers. This is possible because C# makes a custom parametric instantiation for each different parametric 'size' that is required. This means when you instantiate a C# List<int>, the underlying array list stores densely packed arrays of int.

来源: http://www.pin5i.com/showtopic-24376.html

Java 有几种原始类型(int,long,double,byte等) - 但是,它们是特别之处在于它们不是面向对象的,并且它们无法使用语言本身进行定义。它们是值类型,而不是堆分配,并按值传递。

Java also has several primitive types (int, long, double, byte, etc) - however, they are special in that they are not object-oriented and they could not have been defined using the language itself. They are value types, not heap allocated, and passed by value.

来源: C#和Java的比较 - 统一类型系统(维基百科)

同时, Java也有面向对象的原始包装器类型(整数,长,双,字节等),通常称为盒装类型。这些是堆分配的对象,它们通过引用传递,并且与上面提到的基本类型并行存在。

At the same time, Java also has object-oriented primitive "wrapper" types (Integer, Long, Double, Byte, etc), often called boxed types. These are heap allocated objects which are passed by reference, and exist in parallel to the primitive types mentioned above.

在Java的更新版本中,原始类型会自动装入必要时对象类型。这减轻了管理它们的大部分负担,但它也可能导致细微的错误(另见自动装箱)。

In more recent versions of Java, primitive types are automatically boxed into object types when necessary. This relieves most of the burden of managing them but it can also cause subtle bugs (see also auto-boxing).

与C#相反,在Java中,内置的JDK Collections框架始终管理对象指针的集合。为了使它们以向后兼容的方式进行参数化,Java执行一种称为类型擦除的技术,其中(在运行时)所有内容都被视为容器内的对象(参数化类型检查在编译时执行)。

In contrast to C#, in Java, the built-in JDK Collections framework always manages collections of object pointers. In order to make them parametric in a backward-compatible fashion, Java performs a technique called type-erasure, where (during runtime) everything is treated as an object inside the container (parameterised type checks are performed at compile time).

这意味着您无法创建Java List< int> ,您只能使 List<整数> 。并且,上面的列表实际上存储了一个指向盒装 Integer 对象的指针数组,这个对象的大小是C#版本的两倍,性能也大大降低。对于大多数用例,这种大小和性能的差异是无关紧要的。

This means that you cannot make a Java List<int>, you can only make List<Integer>. And, the list above actually stores an array of pointers to boxed Integer objects, which is double the size and substantially less performant than the C# version. For most use cases, this difference in size and performance is irrelevant.

在大小和性能相关的用例中,有两种选择:

In use cases where size and performance are relevant, there are two options available:


  1. 如果事先知道列表的大小,请使用本机类型数组,例如 int [] 。本机类型的数组被打包在内存中,因此它们消耗的内存更少,性能更高。

  2. 如果您事先不知道列表的大小,请使用一些第三方列表实现包装原生数组,可以在创建后添加元素(例如: Trove Colt Fastutil 番石榴)。

  1. When you know the size of your list in advance, use an array of native types, for example int[]. Arrays of native types are packed in memory so they consume A LOT less memory and are more performant.
  2. When you do not know the size of your list in advance, use some 3rd party list implementation that wraps the native array, making it possible to add elements to it after creation (some examples: Trove, Colt, Fastutil, Guava).

这篇关于什么是统一型系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆