-3

I have some problems with generic types. I don't understand why in this code all the calls to myTest point at:

public static <MyData> void myTest(Integer integer)

In some cases I specify the type between angle brackets:

tr.<Number>myTest(null); OR tr.<String>myTest(null);

But I expect that calls go to other methods.

this is my class:

public class MyTest {

    public static void main(String[] arg){
        MyTest tr = new MyTest();

        tr.<Number>myTest(null);

        tr.<MyTest>myTest(null);

        tr.<String>myTest(null);

    } 

    public static <Number> void myTest(Number number){
    }
    public static <MyData> void myTest(Integer integer){
    }
    public static           void myTest(String string){
    }
    public static <String> void testClass(String string){
    }
}

Thanks in advance!

Marquake
  • 191
  • 1
  • 3
  • 10
  • Hmm. If it's ambigious, the code should not even compile. – Thilo Apr 04 '16 at 08:25
  • 2
    `` makes a type variable called `Number`. It's no longer referring to `java.lang.Number`. Just remove the ``. (Similarly for other methods). – Andy Turner Apr 04 '16 at 08:27

2 Answers2

2
public static <Number> void myTest(Number number){
}

Here, <Number> creates a type variable called Number - you are no longer referring to java.lang.Number. This code is semantically equivalent to writing:

public static <T> void myTest(T number){
}

so it's basically the same as writing

public static void myTest(Object number){
}

(assuming you never need to refer to Number/T in the body of the method).

As such, all three of the method invocations

tr.<Number>myTest(null);
tr.<MyTest>myTest(null);
tr.<String>myTest(null);

are simply invoking the same method with different generic constraints.

If you want a different method for each type, don't use generics. Just define a non-generic overload for each of the types:

void myTest(Number n) { ... }
void myTest(MyTest n) { ... }
void myTest(String n) { ... }

and then invoke with an explicit cast of the null (or a reference of the particular type):

myTest((Number) null);
myTest((MyTest) null);
myTest((String) null);
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • Then which is the use for ? If I put "Number", I want to say "Object", If I put Integer, I want to say "Object", etc... I read java documentation and I understand that you can call to any method without . I think that is not right but all says my that is useless... :( I need to clear this. Thanks. – Marquake Apr 04 '16 at 08:51
  • You use type variables when you need to constrain the type. For instance, one use case is where you need two (or more) variables to be of the same type: ` void myTest(T paramA, T paramB) {}`. Another is where you need the return type to match a param: ` T myTest(T param) { return param; }`. Another is where you need to place a bound on the type: ` void myTest(T someNumber)`. Another is where you actually need to refer to type in the body. – Andy Turner Apr 04 '16 at 08:54
  • Ok. here I difference for the parameters: `tr.myTest(new String ("A"), new String("B")); tr.myTest(new String ("A"), new Integer("1")); tr.myTest(new String ("A"), null); tr.myTest(new Integer("1"), new Integer("1")); void myTest(T paramA, T paramB){ } void myTest(String paramA, T paramB){ } void myTest(String paramA, String paramB){ } void myTest(Integer paramA, T paramB){ } void myTest(Integer paramA, Integer paramB){ }` Then isn't very often write `tr.`. right? Is more frequent use the parameter's types to discriminate the method called, right? – Marquake Apr 04 '16 at 09:16
  • I found this: The complete syntax for invoking this method would be: `boolean same = Util.compare(p1, p2);` The type has been explicitly provided, as shown in bold. Generally, **this can be left out** and the compiler will infer the type that is needed:** `boolean same = Util.compare(p1, p2);` – Marquake Apr 04 '16 at 09:51
0

Try to define a new variable and then call your method with it. Example:

public static void main(String[] arg) {
     MyTest tr = new MyTest();

     Number number = null;
     tr.<Number>myTest(number);

     MyTest mTest = null;
     tr.<MyTest>myTest(mTest);

     String str = null;
     tr.<String>myTest(str);
}
  • I just understand the use for "tr." or " tr." or "tr.", I don't have any example in which the use of "tr.<>" is usefull... – Marquake Apr 04 '16 at 08:54