Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么在Java7中右边没有遗漏菱形操作符?_Java_Generics_Diamond Operator - Fatal编程技术网

为什么在Java7中右边没有遗漏菱形操作符?

为什么在Java7中右边没有遗漏菱形操作符?,java,generics,diamond-operator,Java,Generics,Diamond Operator,Java7改进了菱形操作符 在Java6中 Map<String, String> myMap = new HashMap<String, String>(); Map myMap=newhashmap(); 在Java7中 Map<String, String> myMap = new HashMap<>(); Map myMap=newhashmap(); 在Java中,从右侧(RHS)的菱形操作符中删除了7个类型。我的问题是为什么

Java7改进了菱形操作符

在Java6中

Map<String, String> myMap = new HashMap<String, String>();
Map myMap=newhashmap();
在Java7中

  Map<String, String> myMap = new HashMap<>();
Map myMap=newhashmap();
在Java中,从右侧(RHS)的菱形操作符中删除了7个类型。我的问题是为什么不从RHS中删除完整的菱形操作。 我知道它会抛出警告,但是Java7也可以删除警告

                    -

 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
-
类型安全:HashMap类型的表达式需要未经检查的转换才能符合
地图
-HashMap是一种原始类型。对泛型类型HashMap的引用应该参数化
我的想法背后的逻辑:-正如我们已经定义的,map将使用字符串作为键,并在LHS上使用MapMyMap作为对象。 这个编译器有足够的信息。那么,如果您完全错过了菱形操作符,为什么它会发出警告呢?我相信一定有原因
在它后面,但我没有得到它?

下面的代码编译并运行时没有错误

SoftReference<String> ref = new SoftReference(new Integer(1));
Object o = ref.get();
System.out.println(o); // prints "1"
SoftReference-ref=新的SoftReference(新的整数(1));
对象o=ref.get();
System.out.println(o);//打印“1”
将创建
SoftReference
的原始实例。“Raw”意味着没有泛型类型检查,这是允许混合泛型和前泛型代码所必需的


通过使菱形运算符隐式化,您将打破它。

这是类型推断和原始类型之间的区别。答案总是“向后兼容”。