本文介绍了什么是“不兼容类型: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

(这旨在对涉及"void"的非常具体的编译错误消息进行规范的问答,该消息使新的Java程序员感到困惑.它无意成为关于各种不同的类型转换"问题的教程.可以在Java中遇到.)

(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.)

推荐答案

快速解答

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

Quick answer

The compiler is telling you are 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 / source code out how the method should be used.

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

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

详细示例

请考虑以下示例:

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

当我使用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方法中指定行的指定位置检测到问题.问题的根本原因不一定在那条线上,而是在那儿,编译器发现某事是错误的.

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

好吧,您很可能已经了解到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调用返回将分配给sumint值.

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

这是不能分配给...的空隙" 错误消息确实的意思.编译器告诉我们代码正在尝试使用已声明为不返回任何结果的方法的结果.那是不可能的. 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无法转换为..."?意思是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    09-03 05:33