[英]Why Diamond operator was not missed from Right hand side in Java 7?
Java 7 has improved the diamond operator Java 7改进了钻石操作员
In Java 6 在Java 6中
Map<String, String> myMap = new HashMap<String, String>();
In Java 7 在Java 7中
Map<String, String> myMap = new HashMap<>();
In Java 7 Types have been removed from diamond operator on Right hand side(RHS). 在Java 7中,类型已从右侧的钻石操作员(RHS)中删除。 My question why don't remove complete diamond operate from RHS.
我的问题为什么不从RHS删除完整的钻石操作。 I know it will throw the warning, But java 7 could have removed the warning also.
我知道它会抛出警告,但java 7也可以删除警告。
-
Type safety: The expression of type HashMap needs unchecked conversion to conform to
Map<String,String>
- HashMap is a raw type. References to generic type HashMap<K,V> should be parameterized
Logic behind my think :- As we have already defined that map will have string as key and object with Map myMap on LHS. 我认为背后的逻辑: - 我们已经定义了地图将在LHS上使用Map myMap作为键和对象。 With this compiler has sufficient info.
有了这个编译器有足够的信息。 So why it throws warning if you miss diamond operator altogether ?
如果您完全错过钻石操作员,为什么会抛出警告呢? I am surethere must be reason behind it but i am not getting it?
我肯定必须有理由背后但我没有得到它?
The following code compiles and runs without error. 以下代码编译并运行,没有错误。
SoftReference<String> ref = new SoftReference(new Integer(1));
Object o = ref.get();
System.out.println(o); // prints "1"
A raw instance of SoftReference
is created. 创建
SoftReference
的原始实例。 "Raw" means that there is no generic type checking, which is required to allow to mix generics with pre-generics code. “Raw”表示没有泛型类型检查,这是允许将泛型与预泛化代码混合所必需的。
By making the diamond operator implicit, you would break it. 通过隐含钻石算子,你会破坏它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.