Java、C 和 C 中循环变量的类型限制++
为什么 Java、C 和 C++(也许还有其他语言)不允许 for 循环变量有多种类型?例如:
for (int i = 0; i < 15; i++)
在本例中我们有一个循环变量 i,它是循环计数器。
但我可能想要另一个变量,其范围仅限于循环,而不是每次迭代。例如:
for (int i = 0, variable = obj.operation(); i < 15; i++) { ... }
我将 obj.operation()
返回数据存储在 variable
中,因为我只想在循环内使用它。我不希望变量保留在内存中,也不希望在循环执行后保持可见。不仅可以释放内存空间,还可以避免由于错误使用变量
而导致的不良行为。
因此,循环变量很有用,但由于其类型限制而没有得到广泛支持。想象一下 operation()
方法返回一个 long 值。如果发生这种情况,我将无法在不进行转换和丢失数据的情况下享受循环变量的优势。下面的代码不能在 Java 中编译:
for (int i = 0, long variable = obj.operation(); i < 15; i++) { ... }
同样,有人知道为什么存在这种类型限制吗?
Why Java, C and C++ (maybe other languages also) do not allow more than one type on for-loop variables? For example:
for (int i = 0; i < 15; i++)
in this case we have a loop variable i, which is the loop counter.
But I may want to have another variable which scope is limited to the loop, not to each iteration. For example:
for (int i = 0, variable = obj.operation(); i < 15; i++) { ... }
I'm storing obj.operation()
return data in variable
because I want to use it only inside the loop. I don't want variable
to be kept in memory, nor stay visible after the loop execution. Not only to free memory space, but also to avoid undesired behaviour caused by the wrong use of variable
.
Therefore, loop variables are useful, but aren't extensively supported because of its type limitation. Imagine that operation()
method returns a long value. If this happens, I can't enjoy the advantages of loop variables without casting and losing data. The following code does not compile in Java:
for (int i = 0, long variable = obj.operation(); i < 15; i++) { ... }
Again, anybody know why this type limitation exists?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
存在此限制是因为您的要求相当不寻常,并且可以通过非常相似(并且只是稍微更详细)的构造来获得。如果您确实想这样做,Java 支持匿名代码块来限制范围:
This limitation exists because your requirement is fairly unusual, and can be gained with a very similar (and only slightly more verbose) construct. Java supports anonymous code blocks to restrict scope if you really want to do this:
你的例子:
是非法的,原因与:
本身是非法的。逗号不会开始新的语句。逗号之前和之后的两个部分都是一个语句的一部分。该语句声明并初始化一个
int
变量列表。好吧,无论如何,这就是行开头的int
标识符告诉编译器的内容。逗号后面的long
标识符是错误的,因为要声明不同类型的变量,必须开始一个新的语句。由于无法在一条语句中声明两种不同类型的变量,因此必须在
for
初始值设定项之外声明其中之一。Your example:
is illegal for the same reason that:
would be illegal by itself. The comma doesn't start a new statement. Both parts, before and after the comma, are part of one statement. This statement is declaring and initializing a list of
int
variables. Well, that's what theint
identifier at the start of the line tells the compiler, anyway. Thelong
identifier after the comma is an error, as to declare variable(s) of a different type, you must start a new statement.Since you cannot declare variables of two different types in one statement, you must declare one of them outside of the
for
initializer.语法是 for(expr;expr;expr) 并且不能在一个表达式中声明两个不同类型的变量。所以它与循环变量没有太大关系。
Well, the syntax is for(expr;expr;expr) and you can't declare two variables of different types in one expression. So it doesn't have much to do with loop variable.
@jsight 是对的,但我认为真正的原因是语法简单。假设 Java 允许在第一个逗号后面添加类型或新的变量名称。由于 Java 不需要前向声明,因此逗号后面的标识符可以是新的变量名称、现有的类名称或以前未遇到过的类名称。现在应该可以处理这个问题了,但是:
(IMO,语法复杂性是 C 尤其是 C++ 的缺点之一。我在两者中都做了很多编码,但我仍然发现语法错误消息有时很难解码。)
@jsight is right, but I think the real reason is syntactic simplicity. Suppose that Java allowed either a type or a new variable name following the first comma. Since Java doesn't require forward declaration, an identifier following the comma could be a new variable name, an existing class name of the name of a Class that has not been encountered before. Now it should be possible to deal with this, but:
(IMO, syntactic complexity is one of the failings of C and especially C++. I've done a lot of coding in both, but I still find the syntax error messages hard to decode at times.)
只是猜测 - 在循环内声明“long 变量”是如此简单,并且由于 Java 的目标是简单,因此设计者可能认为没有必要添加对您想要的支持。
Just guessing - it is so simple to declare "long variable" inside the loop, and since Java is aimed at simplicity, the designers might not feel it is necessary to add support for what you want.
根据规范,初始化for 语句可以是计算并丢弃的语句表达式列表,也可以是单个变量声明语句。一个变量声明语句可以声明多个变量,但它们都是同一类型。
所以你可以这样做:
但在这种情况下变量将被定义为 int 。
According to the specification, the initialization of the for statement can be a list of statement expressions which are evaluated and discarded, or a single variable declaration statement. A variable declaration statement can declare multiple variables, but they all are of the same type.
So you could do this:
But variable would be defined as an int in this case.