简体   繁体   English

为什么在Java 7中右手边没有漏掉Diamond运算符?

[英]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.

相关问题 Java的逻辑OR运算符不评估右侧,尽管右侧具有一元运算符? - Java's logical OR operator does not evaluate right hand side despite that right hand side has a unary operator? Java 7钻石运营商:为什么难以实施? - Java 7 diamond operator: why was it difficult to implement? 为什么在Java 7中使用菱形运算符进行类型推断? - Why diamond operator is used for Type Inference in Java 7? 在Java 6中使用Generics在右侧? - Using Generics on right hand side in Java 6? 赋值语句左侧的Java后缀增量运算符的语义是什么,为什么它们与右侧不同? - What are the semantics of java's postfix increment operator on the left side of an assignment statement and why do they differ from the right side? 在Java 7中使用菱形运算符 - Using diamond operator in Java 7 为什么在 Java 泛型右侧的集合类型没有任何影响? - Why in Java generics right hand side type of the collection does not have any effect? Java:创建对象时,为什么接口不在右侧? - Java: When creating an object, why isn't the interface on the right hand side? 为什么在 Java 的类型转换期间允许在右侧使用(已擦除)泛型类型? - Why is usage of the (erased) generic type in right-hand side allowed during type casting in Java? 为什么菱形运算符不能在Java 7中的addAll()调用中工作? - Why doesn't the diamond operator work within a addAll() call in Java 7?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM