简体   繁体   中英

Difference between `foo()` and `foo(void)`

In Java, is there any difference between this two function declarations?

public void foo() {/*...*/}

public void foo(void) {/*...*/}

Here you can find the answear to this question but for C/C++ . In these languages it makes totally sense the existance of both declaration styles.

But what is the point of this in Java ?

The latter declaration illegal in Java. You can't declare a method like that. You should get an error like this:

Test.java:8: error: <identifier> expected
    public void foo(void) {/*...*/}
                        ^
1 error

So not only is there no point - you simply won't find valid code which attempts to use this style.

你可以试试

public void foo(Void v) {/*...*/}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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