java “不兼容的类型:void 不能转换为……”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42369301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:34:36  来源:igfitidea点击:

What does "Incompatible types: void cannot be converted to ..." mean?

javacompiler-errorstype-conversionvoid

提问by Stephen C

What does the Java compilation message:

Java 编译消息是什么:

"Incompatible types: void cannot be converted to ..." 

mean, and how do I fix it. Some compilers use different wording; e.g.

意思是,我该如何解决。一些编译器使用不同的措辞;例如

"Type mismatch: cannot convert from void to ..."

or

或者

"Incompatible types. Required: ... Found: void 

(This is intended to be a canonical Q&A for a very specific compilation error message involving "void" that confuses new Java programmers. It is not intended to be a tutorial on the various different "type conversion" problems that one can encounter in Java.)

(这是一个规范的问答,针对涉及“void”的非常具体的编译错误消息,它使新的 Java 程序员感到困惑。它不是关于人们在 Java 中可能遇到的各种不同的“类型转换”问题的教程。 )

回答by Stephen C

Quick answer

快速回答

The compiler is telling you are trying to use the "result" of a method that doesn't return a result.

编译器告诉您正在尝试使用不返回结果的方法的“结果”。

Solution:

解决方案:

  1. Read the javadoc for the method you are trying to call (or the source code if you don't have javadocs).

  2. From the javadocs / source code out how the method should be used.

  3. Correct your code to either notuse the (non-existent) result, or to use a different method. Alternatively, change the method you are calling to return a value.

  1. 阅读您尝试调用的方法的 javadoc(如果您没有 javadoc,则阅读源代码)。

  2. 从javadocs/源代码中找出应该如何使用该方法。

  3. 更正您的代码以使用(不存在的)结果,或使用不同的方法。或者,更改您正在调用的方法以返回值。

Detailed example

详细示例

Consider this example:

考虑这个例子:

public class Test {
    private static void add(int a, int b) {
        int res = a + b;
    }

    public static void main(String[] args) {
        int sum = add(1, 1);
    }
}

When I compile this using javac(Java 8), I get the following compilation error.

当我使用javac(Java 8)编译它时,出现以下编译错误。

$ javac Test.java 
Test.java:7: error: incompatible types: void cannot be converted to int
        int sum = add(1, 1);
                     ^
1 error

The compilation error is actually telling us a few things:

编译错误实际上告诉我们一些事情:

  • The compiler has detecteda problem at the indicated position on the indicated line in the mainmethod. The root cause of the problem is not necessarily on that line, but that is where the compiler has figured out that somethingis wrong.

  • It is a type error - hence the "incompatible types" phrase.

  • The incompatibility involves two types: voidand int.
  • The compiler thinksthat the code requires a conversion from voidto int... and that is not possible.
  • 编译器已在方法中指示行的指示位置检测到问题main。问题的根本原因不一定在该行,但毕竟是在编译器已经想通了,什么是错的。

  • 这是一个类型错误——因此是“不兼容的类型”短语。

  • 不兼容涉及两种类型:voidint
  • 编译器认为代码需要从voidint...的转换,这是不可能的。


So what is this voidtype?

那么这个void类型是什么呢?

Well, you will most likely have learned that Java supports two kinds of type: primitive types and reference types. The voidtype is not either of these. It is the "type" that means "there is no value". (If you consider types to be sets of values, then voidis the empty set.)

好吧,您很可能已经了解到 Java 支持两种类型:原始类型和引用类型。该void类型的这些也不。它是“类型”,意思是“没有价值”。(如果您认为类型是值的集合,则void是空集。)

The primary use for the voidtype is used is in method declarations. Look at the declaration of the addmethod above. Notice that we have declared addwith the signature:

void类型的主要用途是在方法声明中使用。看add上面方法的声明。请注意,我们add使用签名声明:

private static void add(int a, int b)

That signature states that the addmethod takes two intmethods, and returns void. That means that the method will return nothing.

该签名指出该add方法采用两个int方法,并返回void. 这意味着该方法将不返回任何内容。

Yet ... we have called it like this:

然而……我们是这样称呼它的:

int sum = add(1, 1);

This is expecting the addcall to return an intvalue that we will assign to sum.

这是期望add调用返回一个int我们将分配给的值sum

This is what the "void cannot be assigned to ..."error message reallymeans. The compiler is telling us that code is trying to use the result of a method that has been declared to return no result. That's not possible. The Java language does not allow you to "pull a value out of the air" to use.

这就是“void 不能分配给...”错误消息的真正含义。编译器告诉我们代码正在尝试使用已声明为不返回结果的方法的结果。那是不可能的。Java 语言不允许您“从空中提取一个值”来使用。



There are potentially two ways to make the compilation error go away:

有两种方法可以消除编译错误:

  1. We could change the declaration of the method so that it does return a value. For example:

    private static int add(int a, int b) {
        int res = a + b;
        return res;
    }
    
  2. We could change the call site so that it does not attempt yo use the (non-existent) return value. For example:

    add(1, 1);
    
  1. 我们可以更改方法的声明,以便它确实返回一个值。例如:

    private static int add(int a, int b) {
        int res = a + b;
        return res;
    }
    
  2. 我们可以更改调用站点,使其不会尝试使用(不存在的)返回值。例如:

    add(1, 1);
    

In this example, either approach would do that. But only one approach (the first one) makes the code work as intended.

在这个例子中,任何一种方法都可以做到这一点。但是只有一种方法(第一种)可以使代码按预期工作。