Description
Your post
public static <T extends Number> void printBinary(T arg)
T , Number. , T . , , Number, ( , , instanceof).
,
print(Long.toBinaryString(arg));
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.");
}