Why can't I explicitly return void from a method?

前端 未结 14 2321
萌比男神i
萌比男神i 2021-01-01 08:23
void run() {
    ...
    if (done) return cancel();
    ...
}

where cancel() return void. This won\'t compile... and I ca

14条回答
  •  旧时难觅i
    2021-01-01 08:54

    It's like writing:

    void v = (void) 1;
    return (v);
    

    So, I think void is not a type in Java. In C++, return cancel(); is legal. As a C++ programmer who is familiar with Java the answer is: Many things are not supported in Java syntax. Maybe for simplicity or readibility.

    Note: A void f() declaration is similar to a procedure f() declaration in pascal and a procedure could not return any value such as functions, so we must call them in a separated statement.

提交回复
热议问题