Java中的花括号是什么意思? [英] What do curly braces in Java mean by themselves?

查看:154
本文介绍了Java中的花括号是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些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);

第一个之后的那些独立花括号是什么语句是什么意思?

What do those stand-alone curly braces after the first if statement mean?

推荐答案

额外括号的唯一目的是提供范围限制。 列表< PExp> copy 只会存在于这些大括号内,并且不会在它们之外。

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.

如果这是生成的代码,我假设代码 - 生成器执行此操作以便它可以插入一些代码(例如此代码),而不必担心它插入了多少次列表< PExp>复制,如果此片段多次插入同一方法,则无需担心可能重命名变量。

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屋!

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