Java 中的花括号本身是什么意思? [英] What do curly braces in Java mean by themselves?
问题描述
我有一些 Java 代码以两种方式使用花括号
I have some Java code that uses curly braces in two ways
// Curly braces attached to an 'if' statement:
if(node.getId() != null)
{
node.getId().apply(this);
}
// Curly braces by themselves:
{
List<PExp> copy = new ArrayList<PExp>(node.getArgs());
for(PExp e : copy)
{
e.apply(this);
}
}
outAMethodExp(node);
第一个 if
语句后面的独立花括号是什么意思?
What do those stand-alone curly braces after the first if
statement mean?
推荐答案
额外大括号的唯一目的是提供范围限制.List
将只存在于这些大括号内,并且在它们之外没有任何作用域.
The only purpose of the extra braces is to provide scope-limit. The List<PExp> copy
will only exist within those braces, and will have no scope outside of them.
如果这是生成的代码,我假设代码生成器会这样做,因此它可以插入一些代码(例如这个)而不必担心它插入了多少次List
并且不必担心如果此代码段多次插入同一个方法中可能会重命名变量.
If this is generated code, I assume the code-generator does this so it can insert some code (such as this) without having to worry about how many times it has inserted a List<PExp> copy
and without having to worry about possibly renaming the variables if this snippet is inserted into the same method more than once.
这篇关于Java 中的花括号本身是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!