Java的分隔符
无论是对这个概念一无所知却沉迷于每一个程序的程序员。分隔符帮助我们定义程序的结构。在Java,很少有字符用作分隔符。 Java最常用的分隔符是分号(;)。让我们借助“Hello World”程序的插图进行更多探索,以更加强调分隔符。
插图:你好世界
1. // Java Program to Illustrate Separators in Java
2.
3. // Main class
4. class GFG {
5.
6. // Main driver method
7. public static void main (String[] args) {
8.
9. // Print statement
10. System.out.println("Hello World");
11. }
12. }
得出的观察结果如下:
- 在第 3 行中, '{'用于定义块类 GFG 的开始
- 在第 6 行中, '{'用于定义块 main 方法的开始
- 在第 10 行, ';'用于终止句子
- 第 11 行, '}'用于定义块 main 方法的结尾
- 在第 12 行中, '}'用于定义块类 GFG 的结尾
这些分隔符如下图所示:Symbol Name Purpose ( ) Parentheses used to contain a list of parameters in method definition and invocation. also used for defining precedence in expressions in control statements and surrounding cast types { } Braces Used to define a block of code, for classes, methods and local scopes Used to contain the value of automatically initialised array [ ] Brackets declares array types and also used when dereferencing array values ; Semicolon Terminates statements , Comma Used to separates package names from sub-package and class names and also selects a field or method from an object . Period separates consecutive identifiers in variable declarations also used to chains statements in the test, expression of a for loop : Colon Used after labels