Java怪异语法,用于Java中奇怪的显式类型参数声明语法

本文探讨了在Java中遇到的关于显式声明泛型类型的特殊情况,特别是在Guava库的使用中。当编译器无法正确推断类型或者需要更具体的类型参数时,显式类型声明变得必要。尽管通常可以依赖类型推断,但在某些复杂场景下,如集合装饰、同步包装和创建特定类型的映射时,显式指定类型参数是避免编译错误的解决办法。尽管这可能看似冗余,但它是Java泛型系统的一个特性,有助于确保代码的正确性和清晰性。
摘要由CSDN通过智能技术生成

I recently came upon the strange syntax for explicitly declaring generic types when calling Java methods. For example:

Collections.emptyList();

returns an empty List. However, this seems silly as the implementation of emptyList() is just the unchecked type cast (List) EMPTY_LIST, such that all results have the same type erasure (and are the same object.) Moreover, this sort of explicit type declaration is usually not needed because the compiler can often infer the types:

List empty = Collections.emptyList();

After doing some more digging I found two other times where you'd want to use this syntax, and they're all due to using the Guava library and apparently trying to put too many statements on one line.

Decorating a collection, for example with a synchronized wrapper, and the compiler being not able to infer the types. The following doesn't work if you take out the type declaration: cannot convert from Set to Set:

Set set = Collections.synchronizedSet(Sets.newHashSet());

Getting less specific type parameters when they compiler tries to make ones that are too specific. For example, without the type declaration the following statement complains as well: cannot convert from Map to Map:

Map toJson = ImmutableMap.of("foo", "bar");

I find it ironic that in the first case the inferred type parameters are too general and in the second case they are too specific, but I suppose that is just an artifact of the generics system in Java.

However, this language construct itself seems to be avoidable except in these strange use cases invented by the Guava team. Moreover, it seems plain to me that there is a way for the compiler to infer type arguments in both the above examples, and the developers just chose not to do so. Are there examples of it ever being necessary or useful to use this construct in Java programming or does it exist solely to make the compiler simpler / JDK developer's life easier?

解决方案

How is "shutting up the compiler" not "necessary or useful?" I find it both necessary and useful for my code to compile.

There are times when the correct type cannot be inferred, as you have already found. In such cases, it is necessary to explicitly specify the type parameters. Some examples of the compiler just not being smart enough:

And if you really want to dig into the complexities of type inference, it starts and ends with the Java Language Specification. You'll want to focus on JLS §15.12.2.7. Inferring Type Arguments Based on Actual Arguments and §15.12.2.8. Inferring Unresolved Type Arguments.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值