Type Inference from Java 7

Every good programmer likes to write a concise but effective and optimized code. Type Inference is a way introduced in JDK 7 which will surely give you benefits of less typing. Its been a long time that you have using the java code in following manner.

But have you ever thought of code duplication while initializing the specific implementation of Collections? Why there is a need to write the parameters two times during an intialization?

List<string> names = new ArrayList<string>();
Map<string, Object> objectMap = new HashMap<string, Object>(); 
Now most of you would be thinking of initializing as a raw types as you had been doing in previous JDK version.

Something like this.

List<string> names = new ArrayList();
Map<string, object=""> objectMap = new HashMap();

So whats new in JDK 7? What benefits you will have from the new feature?
So first we need to understand the difference between raw type and generic type intialization.

A statements like this ensures that the implementation will contain the same parameter as specified during initialization.

List<string> names = new ArrayList<string>();

In the following example, the compiler generates an unchecked conversion warning because the HashMap() constructor refers to the HashMap raw type, not the Map<String, List<String>> type:

Map<String, List<String>> myMap = new HashMap(); // unchecked conversion warning

Diamond Operator
 
Okay Now I will introduce the new feature of JDK 7.
So we have something called Diamond operator in JDK 7 which reduces your extra typing while initialization.

Syntax:

List<string> names = new ArrayList<>();

So it not only reduces your code but also ensures the type checking.
Here is a more clear example explaining the benefits of type inference.

Advanced Example:

class Demo {
void printStudentNames(List<string> names) {
for(String name:names) {
System.out.println("String name:"+name);
}
}

public static void main(String[] args) {
Demo demo = new Demo();
demo.printStudentNames(new ArrayList<>());   // It saved typing here in a method call too.
List<string> names = new ArrayList<>();
printStudentNames(names);
List<string> copyOfNames = new ArrayList<>(names);  // It saved typing here in a copy contructor invocation too.
}
}

Now what are its limitations?
It won’t work in the case you use wildcards.

Something like this

Class Tree<t> {

public void demoFunction(List<t> objList) {
List<t> copyOfNames = new ArrayList<t>(objList);   //This is not gonna work.
}
}

In the above case the arguments passed in the copy constructor should be Collection<? extends T>
So it wont accept the above inference type.

Reference: Why do we need Type Inference from Java 7? from our JCG partner Saurab Parakh at the Coding is Cool blog.

Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

Leave a Reply


× 2 = eighteen



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books