“不兼容的类型:void 无法转换为 ..."是什么意思?意思是? [英] What does "Incompatible types: void cannot be converted to ..." mean?

查看:20
本文介绍了“不兼容的类型:void 无法转换为 ..."是什么意思?意思是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java编译信息是什么:

What does the Java compilation message:

"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 ..."

"Incompatible types. Required: ... found: void"

"Incompatible types: Found void, required ..."

(这旨在作为一些非常具体的编译错误消息的规范问答,其中涉及void",这往往会使新的 Java 程序员感到困惑.它不打算成为各种不同"Java 中可能遇到的类型转换"问题.)

推荐答案

快速解答

编译器告诉您您的代码正在尝试使用结果".不返回结果的方法.

Quick answer

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

解决方案:

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

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

从 javadocs(或源代码)中,找出应该如何真正使用该方法.

From the javadocs (or source code), work out how the method should really be used.

更正您的代码以使用(不存在的)结果,或使用不同的方法.或者,如果它是您调用的方法之一,另一个可能的更正可能是更改方法以返回适当的值.

Correct your code to either not use the (non-existent) result, or to use a different method. Alternatively, if it is one of your methods that you are calling, another possible correction might be to change the method to return an appropriate value.

详细示例

考虑这个例子:

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);
    }
}

当我使用 javac (Java 8) 编译它时,我得到以下编译错误.

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

$ 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:

  • 编译器在 main 方法中指定行的指定位置检测到一个问题.问题的根本原因不一定在那一行,而是编译器发现 something 是错误的.

  • The compiler has detected a problem at the indicated position on the indicated line in the main method. The root cause of the problem is not necessarily on that line, but that is where the compiler has figured out that something is wrong.

这是一个类型错误——因此是不兼容的类型";短语.

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

不兼容涉及两种类型:voidint.

The incompatibility involves two types: void and int.

编译器认为代码需要从 voidint 的转换...这是不可能的.

The compiler thinks that the code requires a conversion from void to int ... and that is not possible.

嗯,您很可能已经了解到 Java 支持两种类型:原始类型和引用类型.void 类型不是这些.它是类型".这意味着没有价值".(如果你认为类型是一组值,那么 void 就是空集.)

Well, you will most likely have learned that Java supports two kinds of type: primitive types and reference types. The void type 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 void is the empty set.)

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

The primary use for the void type is used is in method declarations. Look at the declaration of the add method above. Notice that we have declared add with the signature:

private static void add(int a, int b)

该签名声明 add 方法采用两个 int 方法,并返回 void.这意味着该方法不会返回任何内容.

That signature states that the add method takes two int methods, and returns void. That means that the method will return nothing.

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

Yet ... we have called it like this:

int sum = add(1, 1);

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

This is expecting the add call to return an int value that we will assign to sum.

这就是 void cannot be assigned to ..." 错误消息真正的意思.编译器告诉我们代码正在尝试使用已声明为不返回结果的方法的结果.那是不可能的.Java 语言不允许您从空中提取值".使用.

This is what the "void cannot be assigned to ..." error message really means. 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.

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

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

  1. 我们可以改变方法的声明,让它返回一个值.例如:

  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;
}

  • 我们可以更改调用站点,使其不会尝试使用(不存在的)返回值.例如:

  • We could change the call site so that it does not attempt yo use the (non-existent) return value. For example:

    add(1, 1);
    

  • 在此示例中,任何一种方法都可以做到这一点.但只有一种方法(第一种)使代码按预期工作.

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

    这篇关于“不兼容的类型:void 无法转换为 ..."是什么意思?意思是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆