具有自动装箱和取消装箱的泛型 [英] Generics with autoboxing and unboxing of primitives

查看:187
本文介绍了具有自动装箱和取消装箱的泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么Generics Java不会进行原语的自动装箱和拆箱.

Why autoboxing and unboxing of primitives not happens with Generics Java.

public static <T extends Number> T addNumber(T a , T b)
{
  int c = a*b;
  System.out.println(c);
  return c; 
}

这就是为什么*操作无法执行以及为什么无法返回c.任何帮助都是可取的.

Here why * operation can't be performed and why can't return c.Any help would be appreciable.

推荐答案

不应将泛型与原始类型一起使用. T表示应作为对象的类型参数.
更多参考
为什么Java泛型不支持原始类型? Java泛型? ,E和T有什么区别?
泛型限制

Generics are not supposed to be used with primitive types. T indicates a type parameter which should be an object.
More reference
Why don't Java Generics support primitive types? Java Generics ? , E and T what is the difference?
Restrictions on generics

这篇关于具有自动装箱和取消装箱的泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆