简体   繁体   English

为什么我在写';'时没有收到任何警告或错误?

[英]Why I am not getting any Warning or Error when writing ';'?

I have written these three lines below inside my Java code: 我在Java代码中写了以下这三行:

ArrayList<String> QuestionID = new ArrayList<String>();
;
ArrayList<String> QuestionType = new ArrayList<String>();

It compiles and runs perfect without any problems. 它编译和运行完美没有任何问题。

Why does it do so? 它为什么这样做?

I cannot understand why I am not getting any warning or error in second line. 我无法理解为什么我在第二行没有收到任何警告或错误。

Somebody decided that an empty statement is valid. 有人认为空语句是有效的。 That's all there is to it. 这里的所有都是它的。 Allowing empty statements is sometimes useful, for instance for for loops with empty bodies. 允许空语句有时很有用,例如对于具有空主体的for循环。

As an example, here's how to find the root node of a tree (assuming there's a parent linkage, and that the root node has no parent): 例如,以下是如何查找树的根节点(假设存在父链接,并且根节点没有父节点):

for (Node parent = anyChildNode; parent.getParent() != null; parent = parent.getParent())
    ; // this for loop has no body, so an empty statement takes its place

Because you are writing an empty statement , which is perfectly valid. 因为您正在编写一个完全有效的empty statement

Here you will find some more explanation. 在这里你会找到更多的解释。

From the above link - 从以上链接 -

The usefulness of this type of statement is limited. 这种陈述的用处是有限的。 The main use that I can think of is to fulfill the statement required for a loop structure. 我能想到的主要用途是完成循环结构所需的语句。

Here is an example that I recently used: 这是我最近使用的一个例子:

while ( sf(++n) != i) ; while(sf(++ n)!= i);

This loop will constantly call the method sf with increasing values of n until the return value of sf(n) is equal to i. 该循环将不断调用方法sf,增加n的值,直到sf(n)的返回值等于i。 Each loop in Java must have some code to execute in the loop body. Java中的每个循环都必须有一些代码才能在循环体中执行。 In this case, all necessary work is done in the condition, and so the mandatory loop body is an empty statement. 在这种情况下,所有必要的工作都在条件中完成,因此强制循环体是一个空语句。

While there may be other (more clear) ways of writing this bit of code, this is an example of where that empty statement can be used. 虽然可能有其他(更清楚)的方法来编写这段代码,但这是一个可以使用该空语句的示例。

Not only is it allowed, but it has its own section in the JLS . 它不仅被允许,而且它在JLS中自己的部分 As expected: 正如所料:

An empty statement does nothing. 空语句什么都不做。

Even more: 更:

Execution of an empty statement always completes normally. 执行空语句总是正常完成。

Which means that the following code: 这意味着以下代码:

;

will never throw an exception. 永远不会抛出异常。 Good to know ;-) 很高兴知道 ;-)

#if DEBUG
    #define ASSERT(_x) Assert(x)
#else
    #define ASSERT(_x)
#endif

ASSERT(test);    // Results in null statement in non-debug builds

Empty semicolon means there is "empty statement" before that. 空分号表示之前有“空语句”。 Thats perfectly valid case. 这完全有效的情况。

; means "execute nothing"; 意思是“什么都不执行”;

Good article about usage of empty statement 关于空语句用法的好文章

";" “;” is considered as a line code without any instructions. 被视为没有任何指示的行代码。

There's no syntax error. 没有语法错误。

它是一个有效的语句,类似于指示编译器什么都不做。

Empty statement came to Java from C where you could find it very useful for things like 空语句从C来到Java,你可以发现它非常有用

strcpy(char *s, char *t) {
    while(*s++ = *t++);
}

Empty Semi Colon是JAVA中的有效令牌,它表示空语句。

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

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