简体   繁体   English

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

[英]What do curly braces in Java mean by themselves?

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 if statement mean? 在第一个if语句之后,那些独立的花括号是什么意思?

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<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> copy and without having to worry about possibly renaming the variables if this snippet is inserted into the same method more than once. 如果这是生成的代码,我假设代码生成器执行此操作,以便它可以插入一些代码(例如this),而不必担心插入List<PExp> copy ,而不必担心可能重命名如果此代码段多次插入同一方法中的变量。

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. 我认为matt b写的是什么,我将补充说,我见过的匿名大括号的另一个用途是在匿名类中声明一个隐式构造函数。 For example: 例如:

  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 look unfamiliar, 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. 由于他们看起来不熟悉,我自己并不是一个很大的粉丝,但至少要认识到如果遇到这种用途会发生什么事情是值得的。

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. 他们使用它来折叠逻辑部分中的代码,这些部分不属于通常折叠的函数,类,循环等。

I'd actually guess that someone forgot an else statement. 我猜想有人忘了别的陈述。

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. 在这种情况下,大多数情况下,有人可能忘记输入控制语句的可能性远远超过他们做一些聪明的事情。

They make an inner scope. 他们构成了内在的范围。 Variable declared inside these braces is not visible outside of them. 在这些括号内声明的变量在它们之外是不可见的。 This also applies to C/C++. 这也适用于C / C ++。

Braces are also useful to reduce the scope in switch/case statements. 大括号对于缩小switch / case语句的范围也很有用。

switch(foo) {
  case BAR:
     int i = ...
     ...
  case BAZ:
     int i = ... // error, "i" already defined in scope
}

But you can write 但你可以写

switch(foo) {
  case BAR:{
     int i = ...
     ...
  }
  case BAZ:{
     int i = ... // OK
  }
}

它也用于初始化块

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

它们定义了一个新范围,这意味着在此范围内声明的所有内容在花括号外部都不可见。

The bring a scope, copy will 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 copy serves as a temporary variable, so it is a good example. 在这种情况下, copy用作临时变量,因此它就是一个很好的例子。

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; } if(a) { int f; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM