List<Long> 如何引用具有 BigInteger 值的 ArrayList

How can a List<Long> references to an ArrayList which has BigInteger values

此方法在 JpaRepository 中定义并运行本机 PostgreSQL 查询。

List<Long> distributorIds = distributorRepository
.findDistributorIdsWithChildren(distributorId)

它无一例外地运行并且在运行时我在返回的 distributorIds ArrayList 中看到 BigInteger 值而不是 Long 值

同题:

那么这个bug怎么会出现呢?我的意思是 JAVA 如何允许这个?如果 Java 不检查这种类型错误,那不是 JAVA 中泛型的问题。

注意:我还检查了 Long 和 BigInteger 的类型层次结构,没有 sub/super class 关系。

通用类型检查是一种编译时功能。在运行时,所有类型信息都会丢失。参见 "type erasure"。例如,如果将使用非泛型集合的遗留 API 映射到使用泛型并需要对集合进行强制转换的 API,则您看到的行为很容易发生。如果该集合恰好包含意外类型的对象,遗憾的是,您只能在运行时才能发现。