为什么我不能从一个void函数中返回void。

huangapple go评论71阅读模式
英文:

Why cannot I return void from a void function

问题

以下是您要翻译的内容:

为什么这段代码有效:

void hello()
{
hello();
return;
}

而这段代码无效:

void hello(){return hello();}

错误信息:

java.java:13: 错误: 不兼容的类型:意外的返回值
return hello();

(请忽略逻辑错误)

主要问题:为什么我们不能将void返回给void函数?

Java是否以任何方式提供对另一种类型的void的支持,或者可能会有一个名为Void的包装类?

英文:

Why does this code work

void hello()
{
hello();
return;
}

while this does not

void hello(){return hello();}

Error:

java.java:13: error: incompatible types: unexpected return value
return hello();

(Please ignore the logical error)

The main question: Why cannot we return void to a void function?

Does Java by any means provide support another type of void, maybe a wrapper class called Void?

答案1

得分: 1

主要问题是:为什么不能在 void 函数中返回 void?

因为 void 函数不返回任何值,因此您不能 return 任何内容。
在 void 方法上下文中,return; 表示的只是“结束方法执行”或“退出方法”,而不是“返回空”。

英文:

> he main question: Why cannot we return void to a void function?

Because void function does not return a value thus you cannot return anything.
In void method context return; is just "finish method execution" or "quit method", not "return nothing";

答案2

得分: 1

关于一行代码,尝试这样写:

void hello() { hello(); }

而不是

void hello() { return hello(); }
英文:

As for one-liners, try this:

void hello() { hello(); }

istead of

void hello() { return hello(); }

答案3

得分: 1

有一种方式

class A {
  Void a() {
    // ...
    return a();
  }
}

但是 java.lang.Voidvoid 类型的不可实例化表示,意味着您不能创建其实例(而且,可以理解的是,您也不应该这样做)。最终,您需要返回一个值,它可以是 null - 这是我能想到的唯一“合法”的情况。

它在 泛型和反射 API 中有应用,但我怀疑在这里使用它是为了让递归方法变得更加花哨(?)。

class A {
  Consumer<String> a() {
    return System.out::println;
  }
}

您可能想要返回一个返回 void 的函数的实例。然后,一个函数式接口 java.util.function.Consumer 可能是一个很好的选择。

实际上,它可以是任何适合您的接口类型。例如,

class A {
  Runnable a() {
    // ...
    return () -> a();
  }
}
英文:

There is a way

class A {
  Void a() {
    // ...
    return a();
  }
}

but java.lang.Void is an uninstantiable representation of the void type meaning you can't make instances out of it (and, sensibly, you aren't supposed to). Eventually, you would need to return a value, it could be a null - the only "legit" one I can think of.

It has applications with generics and the Reflection API, but I doubt it being used here for the purpose of making a recursive method fancier (?).

class A {
  Consumer&lt;String&gt; a() {
    return System.out::println;
  }
}

You might want to return an instance of a function that returns void. Then, a functional interface java.util.function.Consumer might be a good fit.

Actually, it could be any interface of the kind that would suit you best. For instance,

class A {
  Runnable a() {
    // ...
    return () -&gt; a();
  }
}

答案4

得分: 1

使用返回类型 void 时,你不能返回任何东西,但使用返回类型 Void 时,你可以返回 null,例如:

public class Main {
    public static void main(String[] args) {
        hello();
    }

    static Void hello() {
        System.out.println("Hello");
        return null;
    }
}

输出:

Hello

你可以在 https://www.baeldung.com/java-void-type 上找到更多关于 Void 的用法。

英文:

With return type void, you can not return anything but with return type, Void, you can return null e.g.

public class Main {
	public static void main(String[] args) {
		hello();
	}

	static Void hello() {
		System.out.println(&quot;Hello&quot;);
		return null;
	}
}

Output:

Hello

You can find some more uses of Void at https://www.baeldung.com/java-void-type

答案5

得分: 1

因为当一个方法的返回值是 void 时,它不接受任何参数,甚至不接受 void 本身。

return x; 表示控制流正在离开该方法,并且它的结果是 x 的值。

return; 表示控制流正在离开该方法,但没有返回结果。

这段代码不起作用,所以我猜你也不应该期望你的代码能够正常工作。

void fun () {return void;} // 不起作用

请参考这个 答案

英文:

Because when a method's return value is void it does not accept any parameter, even void itself.

return x; indicates that control is leaving the method and that its result is the value of x.

return; indicates that control is leaving the method without a result.

This code does not work so I guess you should not expect yours to work too.

void fun () {return void;} // does not work

Please refer to this answer.

huangapple
  • 本文由 发表于 2020年4月3日 22:29:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/61014145.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定