Java中的'{'符号(大括号)表示什么? [英] What does the '{' symbol (curly-brace) indicate in Java?

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

问题描述

我目前正在学习Java,并且遇到了一个似乎无法回答一个简单问题的问题:

I'm currently learning Java, and was given a question that seems to defy a simple answer:

Java中以下符号的含义是什么?{?

当然,有一些共同的含义:if语句的主体,for循环,其他控制结构.但是,这也可能是方法或初始化程序的开始-它们似乎没有什么共同点.

Of course, there are the common meanings: the body of an if statement, for loop, other control structures. But then it could also be the start of a method, or an initializer - these seem to have less in common.

我已经花了很多时间在Java上下文中搜索该符号的单一定义,但是在这里找不到任何满足要求的东西.

I've spent a good bit of time searching for a single definition for this symbol in the context of Java, but haven't found anything that would suffice here.

什么是Java中花括号指示的全部定义?

推荐答案

您的讲师的问题似乎不太好,因为Java中{符号没有统一的含义.

The question from your instructor seems not so great because there isn't a single unified meaning of the { symbol in Java.

在语句的上下文中,{符号用于表示block语句的开始.这说明了{with if语句,while循环,for循环,do ... while循环,switch语句等的所有用法,从技术上讲,它们仅适用于单个语句,但通常与block语句一起使用:

In the context of a statement, the { symbol is used to denote the start of a block statement. This accounts for all the uses of { with if statements, while loops, for loops, do ... while loops, switch statements, etc., which technically only apply to a single statement but are often used with block statements:

if (x == 0) {
    statementOne();
    statementTwo();
}

在方法或类型(类/接口/枚举/注释)的上下文中,{符号用于表示类或方法的主体的开头:

In the context of a method or type (class/interface/enum/annotation), the { symbol is used to denote the beginning of the body of a class or a method:

public class NewClass {
    ...

    public void foo() {
         ...
    }
}

它也可以在类内部用于声明初始化程序或静态初始化程序块:

It can also be used inside a class to declare an initializer or static initializer block:

class MyClass() {
    static int x;
    static {
        x = somethingHorrible();
    }
};

在数组文字的上下文中,{符号用于表示该文字内使用的元素列表的开头:

In the context of an array literal, the { symbol is used to denote the beginning of the list of elements used inside that literal:

int[] arr = new int[] {1, 3, 7};

这些大括号符号的用法与所有其他用法都不相同.实际上,如果我们为这些不同的上下文中的每一个使用不同的符号,则该语言会很好用.

Each of these uses of the open brace symbol is different from all the others. In fact, the language would work just fine if we used a different symbol for each of these different contexts.

我认为,对您的问题的最佳答案是{用于某些事物组将被视为一个单元的上下文中,无论它是一个块语句(许多语句被视为一个语句),还是一个类(许多语句)方法和字段视为一个对象),方法(许多语句视为一个统一的代码段),初始化程序(很多事情需要一次完成)等.

I think that the best answer to your question is that { is used in contexts where some group of things will be treated as a unit, whether it's a block statement (many statements treated as a single one), a class (many methods and fields treated as a single object), a method (many statements treated as a single unified piece of code), an initializer (many things that need to be done at once), etc.

在大多数情况下,正如评论所指出的那样,花括号引入了一个新的范围.语句块,类主体,初始化器主体和函数主体都引入了新的作用域,牢记这一点绝对重要. (不过,数组初始化不会执行此操作.)

In the majority of these contexts, as the comments have pointed out, the brace introduces a new scope. Statement blocks, class bodies, initializer bodies, and function bodies all introduce a new scope, and that is definitely something important to keep in mind. (Array initialization doesn't do this, though.)

这篇关于Java中的'{'符号(大括号)表示什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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