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

void run() { ... if (done) return cancel(); ... } 

where cancel() return void . This will not compile ... and I can understand why. But if I want to return the void from the void, why not? Instead, I end up writing something like this:

 if (done) { cancel(); return; } 

I'm not looking for suggestions in code style, I want to know why Java explicitly forbids returning this type. Any information appreciated, thanks.

+55
java return-type void
Oct 17 '11 at 2:33 p.m.
source share
14 answers

The return statement with an expression returns the value of this expression. The type cancel() is a void expression - it does not matter.

Logically, you want to execute cancel() and then return - so what you have to say. Two actions (calling cancel() and then returning) are logically different.

Now Java can have a kind of “unit” type instead of void - but this will affect more than just the return values.

+24
Oct 17 2018-11-11T00:
source share

This is an interesting question. Since java uses the return type ( void is the return type), your first statement seems to make sense. I would only take this for a convention. Since void is a placeholder and not an object, it was probably decided to leave it for language consistency or simplicity of the compiler.

From JLS

A return statement without an expression must be contained in the body of the method declared using the void keyword, and not to return any value (§8.4) or to the constructor body (§8.8).

Further

To be precise, a return statement without Expression always terminates abruptly, the reason is returning without a value

+25
Oct 17 2018-11-11T00:
source share

I like to write:

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

So, I think void not type in Java.

In C ++ return cancel(); is legal.

As a C ++ programmer familiar with Java, the answer is this: many things are not supported in Java syntax. Perhaps for simplicity or readability.

Note. The declaration of void f() similar to the declaration of procedure f() in pascal, and the procedure cannot return any value, such as functions, so we must call them in a separate expression.

+9
Oct 17 '11 at 2:56 a.m.
source share

void not a type. However, if you use the void type instead of the void keyword, your code will work, but: you will have to manually return null at all exit points from your method.

+6
Oct 17 2018-11-11T00:
source share

Because you are not returning void . void not a value, so it cannot be returned.

+5
Oct 17 '11 at 2:37 a.m.
source share

This is a tautology. A value of void determines that the method has no return value. Therefore, how can you “return the void” when the void does not return at all?

+4
Oct 17 '11 at 17:27
source share

Short answer

The return cancel() must return a valid value, but the declaration of the void run() method declares that run() does not return a value; therefore, return cancel() in run() is an error. The return (without expression) attempts to pass control to the caller and is used when the return type is void ; therefore not a mistake.

Long answer

JLS The *return* Statement section claims

A return statement without an Expression expression is trying to transfer control to the calling method or constructor that contains it. [...] The return statement with an expression must be contained in a method declaration declared to return a value (§8.4) or a compile-time error occurs. The expression must denote a variable or value of some type T, or a compile-time error occurs. Type T must be assigned (§5.2) to the declared method result type or a compile-time error has occurred.

The JLS Method Return Type section reads:

The return type of a method declares the type of the value returned by the method if it returns a value, or indicates that the method is invalid. Declaring a method d1 with a return type R1 is a return type-replaceable for another method d2 with a return type R2, if and only if the following conditions are true: [...] * If R1 is invalid, then R2 is invalid.

JLS Types, Values, and Variables chapter , first paragraph reads:

The Java programming language is a strongly typed language, which means that every variable and every expression has a type that is known at compile time. Types limit the values ​​that a variable can hold (§4.12), or what an expression can express, restrict the operations supported by these values, and determine the value of the operations.

The JLS section of The Kinds of Types and Values reads:

There are two types of types in the Java programming language: primitive types (§4.2) and reference types (§4.3). There are, respectively, two types of data values ​​that can be stored in variables, passed as arguments, returned by methods, and work with: primitive values ​​(§4.2) and reference values ​​(§4.3).

A few more quotes. The JLS Expression Statements section reads:

Unlike C and C ++, the Java programming language only allows certain forms of expression to be used. Note that the Java programming language does not allow "cast to void" -void is not a type

The JLS Method Body section reads:

If a method is declared invalid, then its body should not contain any return statement (§14.17) that has an expression.

And finally, the JLS Method Declarations section reads:

A method declaration either indicates the type of value returned by the method, or uses the void keyword to indicate that the method does not return a value.

Now that we are putting it all together, we can infer the following:

  • If the return contains an expression, the expression must be evaluated with a valid value.
  • The actual value of the return expression must be a primitive type or a reference type.
  • void not a valid value type.
  • A method declared with a return type of void does not return a value.
  • The void run() method does not return a value.
  • In run() , return , without expression, will happily pass control to the caller.
  • In run() , return some expression is an error because some expression must be a valid value, and run() does not return a value.
+3
Oct 18 '11 at 23:22
source share

return x explicitly means "return the value of x", regardless of what this type is (the type, of course, must still match the return type of any function into which the operator is placed).

void is, strictly speaking, the absence of a type and, therefore, the absence of a value - so it makes no sense to return it, just as it does not make sense (and is not allowed) to declare a void variable.

+2
Oct 17 2018-11-11T00:
source share

Void is not a real type. Void is simply the owner of the place to make the method definition syntax more consistent. This is not a java innovation; it inherits from C.

It is for this reason that the compiler does not allow writing return cancel() , even if the cancel() method is void .

+1
Oct 17 '11 at 2:37 a.m.
source share

void not a type. void in the method definition is simply a placeholder for nothing.

+1
Oct 17 2018-11-11T00:
source share

Interesting idea. The main problem is the language specification, which defines the return statement as consisting of return <expression> . The void method is not an expression, so the construct is not allowed.

You have found that you can replicate functionality by executing the void method and then returning, so there is no real reason to resolve it.

+1
Oct 17 '11 at
source share

From JLS:

The return statement without expression must be contained in the body of the method, which is declared using the void keyword, and not for returning the value or in the body of the constructor

...

A return statement with an expression must be contained in a declaration method declared to return a value or compile time an error occurs. An expression must denote a variable or value of some type T or a compile-time error. Type T must be assigned to the declared method result type or a compile-time error occurs.

+1
Oct 17 2018-11-17T00:
source share

Java grammar does not actually care about the type of method call, so this is not a problem. This should be something further down the chain, in a type checking system. I believe that the main reason is that if an optional grammatically optional statement is included after the return keyword, the system expects the value to be passed. void Of course, this is a type, but there are no values ​​with the void type.

But, of course, none of this explains the answer to your question. As you pointed out, there is no reason why this idiom should not be allowed. But there is also no good reason to resolve it. So he throws up. You can try to rationalize why they did what they did, but that would probably be pointless.

+1
Oct 17 '11 at 10:18
source share

The correct way to handle this:

 void run() { ... if (done) { cancel(); return; } ... } 
0
Oct 19 '11 at 2:26 a.m.
source share



All Articles