为什么可以在没有初始值的情况下声明变量? [英] Why is it possible to declare a variable without an initial value?

查看:185
本文介绍了为什么可以在没有初始值的情况下声明变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Gilles Dowek的编程语言原理

I'm reading Gilles Dowek's Principles of Programming Languanges:

他说也可以在不给它的情况下声明变量初始值以及我们必须小心不要使用已声明没有初始值且尚未赋值的变量。这会产生错误。

He says that it's also possible to declare a variable without giving it an initial value and also that we must be careful not to use a variable which has been declared without an initial value and that has not been assigned a value. This produces an error.

注意:本书的作者提到在Java上声明没有初始值的变量的可能性。

Note: The book's author mentions the possibility of declaring variables without an initial value on Java.

那么,为什么这个变量声明有效?我什么时候开始使用?

So, why is this declaration of variables valid? When am I going to use it?

推荐答案

许多不同语言的原因有很多种。

There are many different reasons for many different languages.

MEMORY

当你声明一个变量时,你需要一些内存来保存变量。这涉及向操作系统的内核询问内存,或某种跟踪记忆的监控程序。简而言之,这可能是一项昂贵的操作。因此,在许多情况下,希望同时分配对象所需的所有内存,然后分配以后必须分配给它的任何值。这样,您可以提高关键部件中程序的性能。这个用例很常见,允许在没有初始化的情况下允许声明的功能。但是,良好实践断言,在所有其他情况下,您应该在分配时初始化变量。

MEMORY
When you declare a variable, you want some memory to hold the variable in. This involves asking the kernel of the operating system for memory, or some kind of monitoring program which keeps track of memory. In short, this can be an expensive operation.Hence, in many cases, it is desirable to allocate all the memory required for the object at the same time, and then assign whatever value has to be assigned to it later. This way, you can increase the performance of the program in the critical parts. This use case is common enough that a feature allowing declaration without initialization is allowed. However, good practices assert that in all other cases you should initialize the variable while assigning.

将内存分配视为官僚机构。纸上工作太多了。因此,如果您知道以后将要使用大量内存,则需要在单个事务中预先提供大量内存,而不是每次都要求内核。

Think of the memory allocation as a bureaucracy. There is too much paper work. So, if you know you are going to use a large amount of memory later, you ask for a large amount of memory upfront in one single transaction, rather than asking the kernel each next time.

昂贵的初始化

这一点与上述内容非常相似。假设您拥有100万次100万个阵列。初始化这样的阵列是一个昂贵的过程。使用默认值这样做将是愚蠢的,因此,这样的功能,其中分配内存然后根据需要使用。

EXPENSIVE INITIALIZATION
This point is very similar to the above point. Suppose you have a 1 million times 1 million array. Initializing such an array is an expensive procedure. To do so with defaults would be stupidity, and hence, such a feature, where memory is allocated and then used as needed.

在这里,它就像你购买了大量的乐高积木来构建一些东西,但你想以默认的蜘蛛侠的形状购买它们。店主或者你将不得不额外努力让他们成为蜘蛛侠的形状,当你无论如何重新塑造他们。

In here, its like you are buying a huge amount of lego blocks to construct something, but you want to buy them in shapes of the default spiderman. The shopkeeper or you would have to extra hard to get them in shapes of spiderman when you are anyway going to reshape them later.

这篇关于为什么可以在没有初始值的情况下声明变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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