Avoid primitives in API design?

I am developing an API. It will have many methods that do the same, but have different parameter primitives.

public void someMethod1(int x);
public void someMethod1(float x);
public void someMethod1(double x);
public void someMethod2(int x, int y);
...
public void someMethod3(int x, int y, int z);
...

Because of the primitives, I have to copy and paste a lot, which, in my opinion, can not be controlled over time. Is it a good idea to avoid primitives in methods and constructors? For example, replacing the above would be:

public <T extends Number> void someMethod1(T x);
public <T extends Number> void someMethod2(T x, T y);
public <T extends Number> void someMethod3(T x, T y, T z);

Edit:

What are the disadvantages of this?

+3
source share
4 answers

- autoboxing/autounboxing Java 1.5 . int , Integer, , . .

, , , Number. , .

- , , , . .

List, , , List, . List , , API Collections ..

+4

List<T> vs. T[], .

List<T> , T[], List .

, Iterable<T> , , .

+2

API- , , ( API) , , API.

int addOne(int integer) , .

Employee getEmployee(int empID) , , , .

+1

, model-view-controller. , int -128 127, Java, Integer.valueOf(int) Integer. , java.lang.Integer.IntegerCache.high, 127. , , - Integer, . , .

+1

Source: https://habr.com/ru/post/1735811/


All Articles