“类型安全”是什么意思?


当前回答

“类型安全”的编程语言意味着以下几点:

不能从未初始化的变量中读取 数组的索引不能超出它们的边界 不能执行未检查的类型强制转换

其他回答

类型安全意味着在编程上,变量、返回值或参数的数据类型必须符合特定的标准。

实际上,这意味着7(整数类型)不同于"7"(字符串类型的引号字符)。

PHP, Javascript和其他动态脚本语言通常是弱类型的,因为它们将转换(字符串)如果您尝试将“7”+ 3添加为“7”到(整数)7,尽管有时您必须显式地这样做(Javascript使用“+”字符进行连接)。

C/ c++ /Java不会理解,或者会将结果连接到“73”中。类型安全通过明确类型需求来防止代码中出现这些类型的错误。

类型安全非常有用。上述“7”+ 3的解决方案是类型转换(int)7 + 3(等于10)。

概念:

为了像含义一样非常简单的类型安全,它确保变量的类型应该是安全的

没有错误的数据类型,例如不能保存或初始化字符串类型的整数变量 不可访问超出绑定的索引 只允许特定的内存位置

所以这都是关于变量存储类型的安全性。

为了更好地理解,请观看下面的视频,该视频演示了类型安全语言(c#)和非类型安全语言(javascript)的代码。

http://www.youtube.com/watch?v=Rlw_njQhkxw

现在来看长文本。

类型安全意味着防止类型错误。当一种类型的数据类型在不知不觉中被分配给另一种类型时,就会发生类型错误,我们会得到不想要的结果。

例如,JavaScript不是一种类型安全语言。在下面的代码中,“num”是一个数值变量,“str”是字符串。Javascript允许我做“num + str”,现在猜测将它做算术或串联。

现在对于下面的代码,结果是“55”,但重要的一点是,它将执行什么样的操作造成了混乱。

这是因为javascript不是一种类型安全的语言。它允许不受限制地将一种类型的数据设置为另一种类型。

<script>
var num = 5; // numeric
var str = "5"; // string
var z = num + str; // arthimetic or concat ????
alert(z); // displays  “55”
</script>

c#是一种类型安全语言。它不允许将一种数据类型分配给另一种数据类型。下面的代码不允许在不同的数据类型上使用“+”运算符。

类型安全不仅是编译时约束,也是运行时约束。我觉得即使过了这么久,我们也可以进一步明确这一点。

与类型安全相关的主要问题有两个。内存**和数据类型(与其对应的操作)。

内存* *

一个字符通常需要每个字符1个字节,或者8位(取决于语言,Java和c#存储unicode字符需要16位)。 int需要4个字节,或32位(通常)。

视觉:

字符 : |-|-|-|-|-|-|-|-|

int : |-|-|-|-|-|-|-|-| |-|-|-|-|-|-|-|-| |-|-|-|-|-|-|-|-| |-|-|-|-|-|-|-|-|

类型安全语言不允许在运行时将int类型插入到char类型中(这会抛出某种类型的类强制转换或内存溢出异常)。但是,在一种类型不安全的语言中,您将覆盖现有数据的相邻内存多3个字节。

Int >> char:

|-|-|-|-|-|-|-|-| |?|?|?|?|?|?|?|?| |?|?|?|?|?|?|?|?| |?|?|?|?|?|?|?|?|

在上面的例子中,右边的3个字节被覆盖,所以任何指向该内存的指针(比如3个连续的字符),希望得到一个可预测的char值现在都是垃圾。这将导致您的程序中出现未定义的行为(或者更糟,可能会在其他程序中出现,这取决于操作系统如何分配内存——目前不太可能)。

**虽然第一个问题在技术上不是关于数据类型的,但类型安全语言固有地解决了这个问题,并且它向那些不知道内存分配“看起来”如何的人直观地描述了这个问题。

数据类型

更微妙和直接的类型问题是两种数据类型使用相同的内存分配。取int型和unsigned int型。两者都是32位。(也可以是char[4]和int,但更常见的问题是uint vs. int)。

|-|-|-|-|-|-|-|-| |-|-|-|-|-|-|-|-| |-|-|-|-|-|-|-|-| |-|-|-|-|-|-|-|-|

|-|-|-|-|-|-|-|-| |-|-|-|-|-|-|-|-| |-|-|-|-|-|-|-|-| |-|-|-|-|-|-|-|-|

类型不安全语言允许程序员引用正确分配的32位span,但当将无符号整型的值读入整型的空间时(反之亦然),我们再次遇到未定义的行为。想象一下这可能会给银行项目带来的问题:

“伙计!我透支了30美元,现在我还剩65,506美元!!”

...当然,银行程序使用更大的数据类型。,)哈哈!

正如其他人已经指出的,下一个问题是对类型的计算操作。这一点已经得到了充分的讨论。

速度vs安全

今天的大多数程序员都不需要担心这些事情,除非他们使用的是C或c++之类的东西。这两种语言都允许程序员在运行时轻易地违反类型安全(直接内存引用),尽管编译器尽最大努力将风险降至最低。然而,这也不全是坏事。

这些语言计算速度如此之快的一个原因是它们不需要像Java那样在运行时操作期间验证类型兼容性。他们认为开发人员是一个很理性的人,不会把字符串和int放在一起,因此,开发人员获得了速度/效率的奖励。

类型安全意味着可以分配给程序变量的值集必须符合定义良好且可测试的标准。类型安全变量导致程序更加健壮,因为操作变量的算法可以相信变量只接受定义良好的一组值中的一个。保持这种信任可以确保数据和程序的完整性和质量。

For many variables, the set of values that may be assigned to a variable is defined at the time the program is written. For example, a variable called "colour" may be allowed to take on the values "red", "green", or "blue" and never any other values. For other variables those criteria may change at run-time. For example, a variable called "colour" may only be allowed to take on values in the "name" column of a "Colours" table in a relational database, where "red, "green", and "blue", are three values for "name" in the "Colours" table, but some other part of the computer program may be able to add to that list while the program is running, and the variable can take on the new values after they are added to the Colours table.

Many type-safe languages give the illusion of "type-safety" by insisting on strictly defining types for variables and only allowing a variable to be assigned values of the same "type". There are a couple of problems with this approach. For example, a program may have a variable "yearOfBirth" which is the year a person was born, and it is tempting to type-cast it as a short integer. However, it is not a short integer. This year, it is a number that is less than 2009 and greater than -10000. However, this set grows by 1 every year as the program runs. Making this a "short int" is not adequate. What is needed to make this variable type-safe is a run-time validation function that ensures that the number is always greater than -10000 and less than the next calendar year. There is no compiler that can enforce such criteria because these criteria are always unique characteristics of the problem domain.

Languages that use dynamic typing (or duck-typing, or manifest typing) such as Perl, Python, Ruby, SQLite, and Lua don't have the notion of typed variables. This forces the programmer to write a run-time validation routine for every variable to ensure that it is correct, or endure the consequences of unexplained run-time exceptions. In my experience, programmers in statically typed languages such as C, C++, Java, and C# are often lulled into thinking that statically defined types is all they need to do to get the benefits of type-safety. This is simply not true for many useful computer programs, and it is hard to predict if it is true for any particular computer program.

长和短....您需要类型安全吗?如果是,那么编写运行时函数来确保当变量被赋值时,它符合定义良好的标准。缺点是它使域分析对于大多数计算机程序来说非常困难,因为您必须显式地为每个程序变量定义标准。