What does “Error: Incompatible types: void cannot

2018-12-31 16:03发布

问题:

What does the Java compilation message:

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

mean, and how do I fix it.

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

回答1:

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

  • The incompatibility involves two types: void and int.
  • The compiler thinks that the code requires a conversion from void to int ... and that is not possible.

So what is this void type? 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.)

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)

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

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

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

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