Java中的花括号本身是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/241088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:56:03  来源:igfitidea点击:

What do curly braces in Java mean by themselves?

javasyntaxscopecurly-braces

提问by Paul Wicks

I have some Java code that uses curly braces in two ways

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

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

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

采纳答案by matt b

The only purpose of the extra braces is to provide scope-limit. The List<PExp> copywill only exist within those braces, and will have no scope outside of them.

额外大括号的唯一目的是提供范围限制。该List<PExp> copy会只有那些括号内的存在,并让他们没有范围之内。

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> copyand without having to worry about possibly renaming the variables if this snippet is inserted into the same method more than once.

如果这是生成的代码,我假设代码生成器会这样做,因此它可以插入一些代码(例如这个),而不必担心它插入了多少次 aList<PExp> copy并且不必担心如果此代码段可能会重命名变量多次插入同一个方法中。

回答by Pawe? Hajdan

They make an inner scope. Variable declared inside these braces is not visible outside of them. This also applies to C/C++.

他们制作了一个内部范围。在这些大括号内声明的变量在它们外部不可见。这也适用于 C/C++。

回答by Pawe? Hajdan

I think they just define an unnamed level of scope.

我认为他们只是定义了一个未命名的范围级别。

回答by fhe

They define a new scope which means that everything declared in this scope is not visible outside the curly braces.

它们定义了一个新的作用域,这意味着在该作用域中声明的所有内容在花括号之外都是不可见的。

回答by Patrick

I'd actually guess that someone forgot an else statement.

我实际上猜测有人忘记了 else 语句。

There's rarely a good reason to even bother with creating additional block scopes. In this, and most cases, it's far more likely someone may have forgotten to type their control statement than it is that they were doing something clever.

很少有充分的理由去创建额外的块作用域。在这种情况下,并且在大多数情况下,更有可能是有人忘记输入他们的控制语句,而不是他们在做一些聪明的事情。

回答by Pavel Feldman

The bring a scope, copywill not be visible outside of it, so you can declare another variable with same name later. And it can be gathered by the garbage collector right after you exit that scope. In this case copyserves as a temporary variable, so it is a good example.

带来一个范围,副本在它之外将不可见,因此您可以稍后声明另一个具有相同名称的变量。它可以在您退出该范围后立即由垃圾收集器收集。在这种情况下,copy用作临时变量,因此它是一个很好的示例。

回答by Hugo

As an interesting note: the braces actually enable a class of statements: declarations.

有趣的是:大括号实际上启用了一类语句:声明。

This is illegal: if(a) int f;

这是非法的: if(a) int f;

but this is legal: if(a) { int f; }

但这是合法的: if(a) { int f; }

回答by Dov Wasserman

I second what matt b wrote, and I'll add that another use I've seen of anonymous braces is to declare an implicit constructor in anonymous classes. For example:

我第二个是 matt b 写的,我要补充一点,我见过的匿名大括号的另一个用途是在匿名类中声明一个隐式构造函数。例如:

  List<String> names = new ArrayList<String>() {
    // I want to initialize this ArrayList instace in-line,
    // but I can't define a constructor for an anonymous class:
      {
        add("Adam");
        add("Eve");
      }

  };

Some unit-testing frameworks have taken this syntax to another level, which does allow some slick things which look totally uncompilable to work. Since they lookunfamiliar, I am not such a big fan myself, but it is worthwhile to at least recognize what is going on if you run across this use.

一些单元测试框架将这种语法提升到另一个层次,这确实允许一些看起来完全无法编译的漂亮东西工作。由于它们看起来很陌生,因此我自己并不是那么大的粉丝,但是如果您遇到这种用法,至少认识到发生了什么是值得的。

回答by Eli

I agree with the scope limit answer, but would add one thing.

我同意范围限制答案,但要补充一件事。

Sometimes you see a construct like that in the code of people who like to fold sections of their code and have editors that will fold braces automatically. They use it to fold up their code in logical sections that don't fall into a function, class, loop, etc. that would usually be folded up.

有时您会在喜欢折叠部分代码并拥有自动折叠大括号的编辑器的人的代码中看到这样的结构。他们使用它在逻辑部分折叠他们的代码,这些逻辑部分不属于通常会折叠的函数、类、循环等。

回答by Gabriel

It is also used for initialization blocks.

它也用于初始化块