Both are compile-time operations and in Java both referes to generics.
The difference is that type inference is used to substitute a type in a generic code, without specifying explicity a type. Type erasure instead is used to replace the generic code with concrete types in the byte code.
Type inference can be used or not with generics, but type erasure is always involved if generics are used.
This instruction doesn't require type inference, because the type Integer is specified:
MyObj.doSomething(myInteger);
If we don't specify the type, the compiler will determine the correct type using type erasure, finding the most specific type that work with all the parameters or return type.
MyObj.doSomething(myInteger);
With Java 7 there is also type inference for the constructor, by using what has been called the diamond:
Mapmap = new HashMap<>();
Copyright © 2013 Welcome to the website of Davis Fiore. All Rights Reserved.