Generic Methods and Type Inferencing in Java

segfault :

Given the following not-very-useful code:

package com.something;

import java.util.ArrayList;
import java.util.Collection;

//Not a generic class!
public class Test {

  public <T> void plain(T param1, T param2) {}
  public <T> void fancy(T param1, Collection<T> param2) {}

  public void testMethod() {

    //No error
    fancy("", new ArrayList<String>());

    //Compiler error here!
    fancy("", new ArrayList<Integer>());

    //No error
    plain("", new ArrayList<Integer>());

  }

}

(Please correct my understanding if it's wrong!)

The 2nd call to fancy() is a compiler error because Java can't infer any common type between the two arguments (can't infer Object since the second parameter must be a Collection.)

The call to plain() is not a compiler error because Java infers the common type of Object between the two arguments.

I recently came across code that had a method signature similar to plain().

My question is this:

Is plain()'s signature useful for anything?

Perhaps the person who wrote that code thought that plain()'s signature would enforce that both parameters have the same type at compile time, which is obviously not the case.

Is there any difference from or benefit to writing a method with a signature like plain() rather than just defining both parameters to be Objects?

erickson :

While the compiler does not infer the generic type one might intend, it will enforce type constraints that are explicitly specified. The following invocation results in a type error.

this.<String>plain("", new ArrayList<Integer>()); /* Compiler error. */

The parameterized method <String>plain(String, String) of type Test is not applicable for the arguments (String, ArrayList<Integer>)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Return Type of Java Generic Methods

From Dev

F# type inferencing

From Dev

Clojure shortfall on type inferencing

From Dev

Rust type inferencing oddity

From Dev

OCaml Type Inferencing

From Dev

Java generic methods, wildcard List return type

From Dev

Haskell for Lambda Calculus, Type Inferencing

From Dev

python 3: type inferencing with mypy?

From Dev

Inferencing with Tensorflow Serving using Java

From Java

Inferencing from tflite model in Java

From Dev

Generic methods type inference

From Dev

Generic methods and type casting

From Dev

Java8 type generic erasures methods signature and lambdas are not working

From Java

Java generic methods cast to parameter type at runtime, is it possible?

From Java

Java Object return type vs. Generic Methods

From Dev

Rust versus Java static methods and invoking on generic type parameters

From Java

Strange type inference behavior of Collections.emptyList() and/or Java generic methods?

From Dev

generic as a generic type in Java?

From Dev

How to make TypeScript type inferencing work for arrays

From Java

Generic Type of a Generic Type in Java?

From Java

Java Overriding Generic Methods

From Java

Java: Generic methods and numbers

From Java

Invoking Java Generic Methods

From Dev

Passing generic type to non generic methods

From Dev

Importing methods with generic type parameters

From Dev

A type mismatch issue with generic methods

From Dev

Pass mapper as type and use methods on generic type

From Dev

How to wrap a class that has generic type methods with a class that has a generic type and no generic type arguments on the methods?

From Java

Java - generic classes hierarchy and generic methods overloading

Related Related

HotTag

Archive