我有一些使用花括号的Java代码,其用法有两种。
// 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
语句后面的那些独立的花括号是什么意思?
copy
可以在outAMethodExp()
返回之前进行垃圾回收,这是一个额外的“好处”。如果这是一个长时间运行或内存密集型的调用,那可能会有所帮助。我用引号括起来的“好处”,是因为将代码重构为单独的方法通常比利用这种语法更清晰明了。 - dimo414