How to make a general method of locking your type in java?

what I mean is that in C #, for example, I can write a generic method like this:

public static void Concatenate<T> (T arg1, T arg2) { Console.WriteLine(arg1.ToString() + arg2.ToString()); } 

and then if I call the method in different ways:

 Concatenate("one", "two"); // will work just fine, outputs "onetwo" Concatenate(1, 2); // will also work great, outputs 12 Concatenate("one", 2) // will give a compiler error 

alternately, I could call the method as follows: Concatenate<string>("one", "two"); and be sure that only lines get ...

now if i try the same thing in java

 public static <T> void concatenate(T arg1, T arg2) { System.out.println(arg1.toString() + arg2.toString()); } 

and call the method exactly the same as in the C # example:

 concatenate("one", "two"); // will work just fine, outputs "onetwo" concatenate(1, 2); // will also work great, outputs 12 concatenate("one", 2) // will work fine and outputs "one2" 

As far as I know, I cannot name a method like Concatenate<string>("one", "two"); as this will give me an error

Is there a way to add the type of security I found in C #?

so I don’t risk being able to just put any type anywhere and just get a warning ...

a better example would be to use variable arguments

in C # I would do:

 public static void QuickSort<T>(params T[] args) // same as (T... args) in java { // code } 

and by calling it, I am sure that only one of the parameters, for example, does the following:

 QuickSort<int>(5, 9, 7, 3, 2, 5, 4, 1); 

whereas in java I could do this:

 quickSort(5, "nine", 7, 3, "two", 5, 4, 1); 

and get nothing but a warning from the IDE, while it will give an error in C #

so my question is: is there a way to β€œblock” the parameter type in java, as I can, in C #, a-la QuickSort<int>(args) , and not quickSort(args) ?

+4
source share
3 answers

As far as I know, I cannot name a method like concatenate<String>("One", "Two") , as this will give me an error

Actually, you can, only the syntax is slightly different:

 public class Main { public static <T> void concatenate(T arg1, T arg2) { System.out.println(arg1.toString() + arg2.toString()); } public static void main(String[] args) { Main.<String>concatenate("one", "two"); // will work just fine, outputs "onetwo" Main.<Integer>concatenate(1, 2); // will also work great, outputs 12 Main.<String>concatenate("one", 2); // will fail at compile time } } 

If concatenate() was not a static method, the syntax would be obj.<String>concatenate(...) .

As for your second example:

 public class Main { public static <T> void quickSort(T... args) { } public static void main(String[] args) { quickSort(5, "nine", 7, 3, "two", 5, 4, 1); // warning Main.<Integer>quickSort(5, "nine", 7, 3, "two", 5, 4, 1); // error } } 

Here Main.<Integer>quickSort(...) fails with the following error:

The parameterized quickSort (Integer ...) method of type Main is not applicable for arguments (Integer, String, Integer, Integer, String, Integer, Integer, Integer)

+4
source

You may be explicit with a general parameter, but the syntax is different from the one you tried:

For instance methods:

 instance.<String>concatenate("a","b") 

For static methods:

 MyClass.<String>concatenate("a","b") 
+2
source

In what you ask, there is no "type security". Concatenate("one", 2) is type safe. There is no reason to prohibit it in terms of type safety. Generics are not intended for arbitrary restrictions.

+1
source

All Articles