In Java, where can a return statement appear?

Also, inside the body of the method / constructor?

Also, is there something that can be returned by the constructor? Or can I use " return;" without any expression after it?

+3
source share
6 answers

In Java, where can a return statement appear? Also, inside the body of the method / constructor?

Only in the constructor or method:

JLS - http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#6767

The returned operator returns control to the calling method (§8.4, §15.12) or to the constructor (§8.8, §15.9) .


Also, is there something that can be returned by the constructor?

No

JLS - http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#6767

return , , (§8.4) .


"return;" - ?

JLS - http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#6767

return Expression , void, - (§8.4), (§8.8)

+7

Java Language Specification, 14.17:

(§8.4, §15.12) (§8.8, §15.9).

ReturnStatement:
  return Expression opt;

return , void, - (§8.4) (§8.8). , (§8.7). return Expression , .

, return Expression , - .

, , return , .

+5

return .

return , . void return .

( ). , return , .

+4

return void. .

    public void test(String s) {
       if (s.equals("")) {
          return;
       }
    }

return , .

EDIT: , , .

0

, ( , ). ( ).

- . VM , , , . return , .

"" ( , - ), .

0

return return . , , . . , , , . . , throw . , , javadoc.

0

Source: https://habr.com/ru/post/1789070/


All Articles