静态类型是否意味着如果要更改变量的类型就必须强制转换变量?

发布于 2024-07-14 21:23:21 字数 130 浏览 6 评论 0原文

在 Java 和 C++ 等静态类型语言中,除了“强制转换”之外,还有其他方法可以更改变量的类型吗?

我试图找出动态类型和静态类型在实践中的主要区别是什么,并不断寻找非常学术的定义。 我想知道这对于我的代码来说意味着什么。

Are there any other ways of changing a variable's type in a statically typed language like Java and C++, except 'casting'?

I'm trying to figure out what the main difference is in practical terms between dynamic and static typing and keep finding very academic definitions. I'm wondering what it means in terms of what my code looks like.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

锦爱 2024-07-21 21:23:21

确保您不会将静态类型与动态类型与强类型与弱类型混淆。

  • 静态类型:每个变量、方法参数、返回类型等都有一个在编译时已知的类型,无论是声明的还是推断的。
  • 动态类型:类型在编译时被忽略/不存在
  • 强类型:运行时的每个对象都有一个特定的类型,并且您只能对其执行为该类型定义的那些操作。
  • 弱类型:运行时对象要么没有显式类型,要么系统尝试在必要时自动转换类型。

这两个对立面可以自由组合:

  • Java 是静态的强类型
  • C 是静态的弱类型(指针算术!)
  • Ruby 是动态的强类型
  • JavaScript 是动态的弱类型

一般来说,静态类型意味着很多错误会被捕获编译器是动态类型语言中的运行时错误 - 但这也意味着您花费大量时间担心类型,在许多情况下是不必要的(请参阅接口与鸭子类型)。

强类型意味着类型之间的任何转换都必须是显式的,无论是通过强制转换还是通过使用转换方法(例如将字符串解析为整数)。 这意味着更多的打字工作,但具有让您能够控制事物的优点,而当系统执行一些模糊的隐式转换时,弱打字通常会导致混乱,这会给您带来完全错误的变量值,从而导致严重破坏十个方法调用线。

Make sure you don't get static vs. dynamic typing confused with strong vs. weak typing.

  • Static typing: Each variable, method parameter, return type etc. has a type known at compile time, either declared or inferred.
  • Dynamic typing: types are ignored/don't exist at compile time
  • Strong typing: each object at runtime has a specific type, and you can only perform those operations on it that are defined for that type.
  • Weak typing: runtime objects either don't have an explicit type, or the system attempts to automatically convert types wherever necessary.

These two opposites can be combined freely:

  • Java is statically and strongly typed
  • C is statically and weakly typed (pointer arithmetics!)
  • Ruby is dynamically and strongly typed
  • JavaScript is dynamically and weakly typed

Genrally, static typing means that a lot of errors are caught by the compiler which are runtime errors in a dynamically typed language - but it also means that you spend a lot of time worrying about types, in many cases unnecessarily (see interfaces vs. duck typing).

Strong typing means that any conversion between types must be explicit, either through a cast or through the use of conversion methods (e.g. parsing a string into an integer). This means more typing work, but has the advantage of keeping you in control of things, whereas weak typing often results in confusion when the system does some obscure implicit conversion that leaves you with a completely wrong variable value that causes havoc ten method calls down the line.

寒尘 2024-07-21 21:23:21

在 C++/Java 中,您无法更改变量的类型。

静态类型:变量在编译类型时分配一种类型并且不会改变。

动态类型:变量的类型可以在运行时更改,例如在 JavaScript 中:

js> x="5" <-- String
5
js> x=x*5 <-- Int
25

In C++/Java you can't change the type of a variable.

Static typing: A variable has one type assigned at compile type and that does not change.

Dynamic typing: A variable's type can change while runtime, e.g. in JavaScript:

js> x="5" <-- String
5
js> x=x*5 <-- Int
25
缘字诀 2024-07-21 21:23:21

主要区别在于,在动态类型语言中,直到在运行时使用某个方法时,您才知道该方法是否存在。 在静态类型语言中,检查是在编译时进行的,如果方法不存在,则编译失败。

The main difference is that in dynamically typed languages you don't know until you go to use a method at runtime whether that method exists. In statically typed languages the check is made at compile time and the compilation fails if the method doesn't exist.

你的笑 2024-07-21 21:23:21

我想知道我的代码的含义是什么。

类型系统不一定会对代码的外观产生任何影响,例如具有静态类型、类型推断和隐式转换的语言(例如 Scala)看起来很像动态类型语言。 另请参阅:讨论类型系统之前需要了解的内容

I'm wondering what it means in terms of what my code looks like.

The type system does not necessarily have any impact on what code looks like, e.g. languages with static typing, type inference and implicit conversion (like Scala for instance) look a lot like dynamically typed languages. See also: What To Know Before Debating Type Systems.

草莓味的萝莉 2024-07-21 21:23:21

您不需要显式转换。 在许多情况下,隐式转换有效。

例如:

int i = 42;
float f = i; // f ~= 42.0
int b = f;   // i == 42

class Base {
};

class Subclass : public Base {
};

Subclass *subclass = new Subclass();
Base *base = subclass;  // Legal

Subclass *s = dynamic_cast<Subclass *>(base); // == subclass.  Performs type checking.  If base isn't a Subclass, NULL is returned instead.  (This is type-safe explicit casting.)

但是,您不能更改变量的类型。 不过,您可以在 C++ 中使用联合来实现某种动态类型。

You don't need explicit casting. In many cases implicit casting works.

For example:

int i = 42;
float f = i; // f ~= 42.0
int b = f;   // i == 42

class Base {
};

class Subclass : public Base {
};

Subclass *subclass = new Subclass();
Base *base = subclass;  // Legal

Subclass *s = dynamic_cast<Subclass *>(base); // == subclass.  Performs type checking.  If base isn't a Subclass, NULL is returned instead.  (This is type-safe explicit casting.)

You cannot, however, change the type of a variable. You can use unions in C++, though, to achieve some sort of dynamic typing.

傻比既视感 2024-07-21 21:23:21

让我们看看静态类型语言 Java 和动态语言 JavaScript。 在Java中,对于对象来说,变量是对对象的引用。 对象具有运行时类型,引用也具有类型。 引用的类型必须是运行时对象或其祖先之一的类型。 这就是多态性的工作原理。 您必须进行强制转换才能向上引用类型的层次结构,但不能向下。 编译器确保满足这些条件。 在像 JavaScript 这样的语言中,变量就是变量。 你可以让它指向任何你想要的对象,并且在你检查之前你不知道它的类型。

不过,对于转换,Java 中有很多方法(例如 toInteger 和 toFloat)来进行转换并生成具有相同相对值的新类型的对象。 JavaScript 中也有转换方法,但它们也会生成新对象。

Lets look at Java for he staitically typed language and JavaScript for the dynamc. In Java, for objects, the variable is a reference to an object. The object has a runtime type and the reference has a type. The type of the reference must be the type of the runtime object or one of its ancestors. This is how polymorphism works. You have to cast to go up the hierarchy of the reference type, but not down. The compiler ensures that these conditions are met. In a language like JavaScript, your variable is just that, a variable. You can have it point to whatever object you want, and you don't know the type of it until you check.

For conversions, though, there are lots of methods like toInteger and toFloat in Java to do a conversion and generate an object of a new type with the same relative value. In JavaScript there are also conversion methods, but they generate new objects too.

污味仙女 2024-07-21 21:23:21

无论您是否使用静态类型语言,您的代码实际上看起来应该没有太大不同。 仅仅因为您可以用动态类型语言更改变量的数据类型,并不意味着这样做是个好主意。

例如,在 VBScript 中,匈牙利表示法通常用于指定变量的首选数据类型。 这样您就可以轻松发现代码是否混合类型。 (这不是匈牙利表示法的最初用途,但它非常有用。)

通过保持相同的数据类型,您可以避免难以分辨代码实际用途的情况,以及代码根本无法正常工作的情况。 例如:

Dim id
id = Request.QueryString("id") ' this variable is now a string
If id = "42" Then
  id = 142 ' sometimes turned into a number
End If
If id > 100 Then ' will not work properly for strings

使用匈牙利表示法,您可以发现混合类型的代码,例如:

lngId = Request.QueryString("id") ' putting a string in a numeric variable

strId = 42 ' putting a number in a string variable

Your code should actally not look very much different, regardless if you are using a staticly typed language or not. Just because you can change the data type of a variable in a dynamically typed language, doesn't mean that it is a good idea to do so.

In VBScript, for example, hungarian notation is often used to specify the preferred data type of a variable. That way you can easily spot if the code is mixing types. (This was not the original use of hungarian notation, but it's pretty useful.)

By keeping to the same data type, you avoid situations where it's hard to tell what the code actually does, and situations where the code simply doesn't work properly. For example:

Dim id
id = Request.QueryString("id") ' this variable is now a string
If id = "42" Then
  id = 142 ' sometimes turned into a number
End If
If id > 100 Then ' will not work properly for strings

Using hungarian notation you can spot code that is mixing types, like:

lngId = Request.QueryString("id") ' putting a string in a numeric variable

strId = 42 ' putting a number in a string variable
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文