加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 为什么这些分号不会产生错误?

发布时间:2020-12-15 04:42:36 所属栏目:Java 来源:网络整理
导读:参见英文答案 What is the use of an empty statement (not for loop) in Java? [closed]????????????????????????????????????6个 我知道分号需要用分号结尾,就像在这段代码中一样: public class Main{ public static void main(String[] args){ System.ou
参见英文答案 > What is the use of an empty statement (not for loop) in Java? [closed]????????????????????????????????????6个
我知道分号需要用分号结尾,就像在这段代码中一样:

public class Main{
    public static void main(String[] args){
        System.out.println("blah blah blah and stuff")
    }
}

在println语句的末尾需要分号,否则程序将无法编译.

最近,我偶然在println语句的末尾放了两个分号:

System.out.println("blah blah blah and stuff");;

我注意到了,所以我开始在我的程序中放置分号

public class Main{;;;;;;
    public static void main(String[] args){;;;;;;;;
        System.out.println("blah blah blah and stuff");;;;;;
    };;;;;;
};;;;;

此代码编译,打印字符串,没有错误或意外结果.我已经搜索了为什么编译器似乎只是忽略了这些分号,但从未找到任何特别提及的内容.那么,为什么这些分号不会产生语法错误?

解决方法

这被称为 empty statement,它是有效的.

An empty statement does nothing.

它没有太多用途

while (checkCondition()) // may have some side effects
    ;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读