Java 8 chooses different overloaded method than Java 7 did when generics are involved [duplicate]

78
February 22, 2018, at 11:00 PM

This question already has an answer here:

  • Why does the Java 8 generic type inference pick this overload? 4 answers

I have the following test class:

import java.io.Serializable;
public class Java8Problem {
    public void test(String stringArg) {
        System.out.println("string-Method taken: " + stringArg);
    }
    public void test(Object objectArg) {
        System.out.println("object-Method taken: " + objectArg.toString());
    }
    public <T extends Serializable> T getTestData() {
        return (T) new Integer(10);
    }
    public static void main(String[] arguments) {
        Java8Problem instance = new Java8Problem();
        instance.test(instance.getTestData());
    }
}

When i compile and run this class in Java 7 the output will be:

object-Method taken: 10

But when i compile and run this class in Java 8 i get a runtime exception:

Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String at Java8Problem.main(Java8Problem.java:21)

So Java 8 seems to takes the most specific method whereas Java 7 takes the most common one.

Does anybody knows if this is a bug in Java 8 or is it changed/desired behaviour? If the latter is the case is there any possibility to configure Java 8 to use the old behaviour? Or is there any other way to solve this?

BTW: i know that the problem here is caused by the return type of method getTestData but this is only a simplified example of my real world problem in which i cannot easily change the signature of that method.

Answer 1

Here's a work-around to get the compiler to choose public void test(Object objectArg) in Java 8, thus avoiding the ClassCastException:

Java8Problem instance = new Java8Problem ();
instance.test(instance.<Integer>getTestData());
READ ALSO
Appium: custom android app keeps crashing on launch

Appium: custom android app keeps crashing on launch

Have a problem where all of a sudden a previously working appium based android app selenium test has stopped working as it did (consistently successful)

557
How to use Calligraphy with multi-language support Oreo

How to use Calligraphy with multi-language support Oreo

I'm developing an app that need to support multiple languages and if the language is RTL I have to apply a custom fontFor the requirement I have created my Class that extends Application

170
How does jfrog artifactory/ any artifactory support ruby gems?

How does jfrog artifactory/ any artifactory support ruby gems?

I am interested to know the internal workings of artifactory and how does it support the gems clientIs the artifactory code for ruby gems written in Ruby or is it in Java

135
JavaFx: ObservableHashSet

JavaFx: ObservableHashSet

I was wondering in FxCollections there are many methods for creating the observable variant of a "simple" Collection like ArrayList or of a HashMap, but I don't find anobservableHashSet for example

184