简体   繁体   中英

Why java allows object declaration to not contain the type

Why java allows object declaration to not contain the type

Map<Integer, Set<Integer>> foos = new HashMap<>();
Map<Integer, Set<Integer>> foos = new HashMap<Integer, Set<Integer>>();

The second declaration is correct than the first, but why doesn't java throw a warning during compilation. How can I force the compiler to throw such errors.

Why java allows object declaration to not contain the type

 Map<Integer, Set<Integer>> foos = new HashMap<>(); Map<Integer, Set<Integer>> foos = new HashMap<Integer, Set<Integer>>(); 

The second declaration is correct than the first, but why doesn't java throw a warning during compilation. How can I force the compiler to throw such errors.

Beginning with Java 7, the two statements above produce exactly the same bytecode when compiled.

Java 7 introduced the diamond operator ("<>") which allows the compiler to perform type inference on type declaration statements. The diamond operator must not be confused with a raw type (which would be specified by no use of angle brackets at all).

In the first statement above, the compiler infers the type <Integer, Set<Integer>> for the newly instantiated HashMap from the type of foos which is declared on the left side of the assignment statement.

The diamond operator is really just a bit of syntactic sugar that allows a bit of shorthand when using the new operator in assignment statements where the type of the new object can be inferred by the compiler.

Type inference is used much more extensively in Java 8 with lambda expressions and the type inference rules for the java compiler were extended to allow type inference to work "like you would expect" in a broader set of situations.

Map<Integer, Set<Integer>> foos = new HashMap<>();

This kind of declaration was introduced in java7(if i am right). Generally After compilation the actual type was filled in place of declarations. So you don't have to worry about the declaration. Its a kind of comfortableness which was introduced in java.

Obviously right hand side will be filled with the same type in LHS.

Older versions of Java didn't support it. Later on, this support was added.

Now, you can declare as (In Java 7, you can do)

Map<Integer, Set<Integer>> foos = new HashMap<>();

instead of

Map<Integer, Set<Integer>> foos = new HashMap<Integer, Set<Integer>>();

In Java5 when generics were introduced and Java6 a warning would be issued on the first declaration. In Java7 the first construction is interpreted as short-hand for the second, saving a lot of redundant typing.

In Java 7 and later you can do

Map<Integer, Set<Integer>> foos = new HashMap<>();

You can use generics to ensure a certain level of type safety:

Map<Integer, Set<Integer>> foos = new HashMap<Integer, Set<Integer>>();

In

Map<Integer, Set<Integer>> foos = new HashMap<>();

<> is known as Diamond Operator . The purpose of this is to reduce the verbosity from the first one (Same type information is mentioned in both the sides) and let the compiler infer the type information from the left hand side itself.

At run time both are same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM