当以下行在 java 中运行\编译时,幕后会发生什么: (LinkedList<Dog>)(Object)(new LinkedList<Car>());?

What happens behind the scenes when the following line is running\compiling in java: (LinkedList<Dog>)(Object)(new LinkedList<Car>());?

提问人:The AppeaRare 提问时间:6/23/2023 最后编辑:The AppeaRare 更新时间:6/23/2023 访问量:55

问:

写作时:

LinkedList<Dog> dogList = (LinkedList<Dog>)(Object)(new LinkedList<Car>());

我没有得到ClassCastException,而不仅仅是写:

Dog dog = (Dog)(Object)(new Car());

为什么会有区别?(幕后发生了什么?

我认为这与 java 的泛型机制有关。

Java 泛型 转换 编译 运行时

评论

3赞 Sören 6/23/2023
我刚刚注意到你标题中的问题与你文本中的问题不同。你想问哪一个?请编辑您的问题以澄清。

答: 暂无答案