Java method accept extension number T

I'm a little rusty and stuck in some kind of elementary problem. I want my method to printBinarytake the values ​​of Longor Integer. and based on the type of input, I want to call the appropriate Object method toBinaryString().

Now I know that there are alternative ways, such as method overloading. Perhaps I am creating two methods: call printBinary, but one accepts Longand the other accepts Integer. But if I want to do it in one way, how can I achieve this?

public static <T extends Object> void print(T arg){
    System.out.println(arg);
}

public static <T extends Number> void printBinary(T arg){
    if(arg instanceof Long){
        print(Long.toBinaryString(arg)); //this throws an error incompatible types: Number cannot be converted to long

    }else{
        print(Integer.toBinaryString(arg)); //this throws an error incompatible types: Number cannot be converted to int

    }
}
+4
source share
4 answers

Description

Your post

public static <T extends Number> void printBinary(T arg)

T , Number. , T . , , Number, ( , , instanceof).

,

// requires long
print(Long.toBinaryString(arg));
// requires int
print(Integer.toBinaryString(arg));

Number, long int. (Long, Integer).

, Java long long Integer int ( ). Number long Number Integer. A Number a long Integer. , , Double. , T, , Number.


, arg long int. , Number, Number # longValue # intValue:

print(Long.toBinaryString(arg.longValue()));
print(Integer.toBinaryString(arg.intValue()));

instanceof

, , . instanceof, T long Integer, :

print(Long.toBinaryString((Long) arg));
print(Integer.toBinaryString((Integer) arg));

Java long long Integer int, unboxing. , unboxing :

print(Long.toBinaryString(((Long) arg).longValue()));
print(Integer.toBinaryString(((Integer) arg).intValue()));

, Integer . arg instanceof Integer, :

if (arg instanceof Long) {
    // Cast to Long is safe
} else if (arg instanceof Integer) {
    // Cast to Integer is safe
} else {
    // Neither Long nor Integer, for example Double
    throw new IllegalArgumentException("Only Long and Integer supported.");
}
+4

,

public static <T extends Number> void printBinary(T arg){
    if(arg instanceof Long){
        print(Long.toBinaryString(arg)); //this throws an error incompatible types: Number cannot be converted to long

    }else{
        print(Integer.toBinaryString(arg)); //this throws an error incompatible types: Number cannot be converted to int

    }
}

, BinaryString (art), ,

public static <T extends Number> void printBinary(T arg){
    if(arg instanceof Long){
        print(Long.toBinaryString((Long) arg)); //this throws an error incompatible types: Number cannot be converted to long

    }else{
        print(Integer.toBinaryString((Integer) arg)); //this throws an error incompatible types: Number cannot be converted to int

    }
}

, . @Jacob H, <T extends Number> , , Number. , , Integer Long, , , , Float. toBinaryString Number, , Integer Long. , , , - Long Integer. , , Integer Long, , , . , Number , .

+2

You just want to use it <Number>, it <T extends Number>means "some type T that extends the number", so it expects either Long or Integer, but not both. <Number>means "Number or any expandable number"

0
source

The best solution is to use an overload method. Using Number, the method will also accept doubles.

If you really want to use Number, you do not need generics. Rewrite methods like this

public static void print(Object arg)

public static void printBinary(Number arg)
0
source

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


All Articles