HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java 7 / Java diamond operator – operator in Java

Java diamond operator – <> operator in Java

Before Java 7, while using generics we had to supply type parameters to variables types and to their actual types. Now, it has been relieved a bit in this new Java 7 feature. And a blank Java diamond operator on right side of declaration will work fine.

Diamond operator are denoted with two angles '< >'.

1. Before generics – raw type declarations

If you have worked on early versions on Java (before 1.5), when generics were not a Java feature, developers have to use raw types declarations and initializations. For example, given below is a HashMap declarations.

Map params = new HashMap();

The problem with this approach is that you can put any object type in key and value, and only in runtime you will get error if object is not of desired type. There is no compile time safety which can warn developers, that which types are allowed and which are not.

2. Generics – parameterized types

JDK 1.5 brought generics. It must much needed feature and completely changed the way developers write the code. It enabled compile time safety. It helped in reducing runtime errors by great number.

Map<String, Integer> params = new HashMap<String, Integer>();

This syntax solves the problem of compile time type safety. In fact, above syntax is good for almost all use-cases. In above example, if you try to add any key or value of different type, compiler will give you error.

You need to fix your code to get through the compiler.

3. Diamond operator

Parameterized types solves issues but seems heavy due to same repeated type information on both sides. We can reduce the syntax if we can provide type information on one side, and other side can detect and apply the type information.

Diamond operator in Java does exactly the same thing. It is also called Elvis operator. Look below at diamond operator syntax.

Map<String, Integer> params = new HashMap<>();

In above code, compiler is smart enough in to identify that diamond operator infer to type defined on left hand side of declaration. It apply the type information to right side object as well. It helps in adding type inference feature to Java.

4. Backward compatibility

Raw types and parameterized types still exist for the sake of backward compatibility. But new compilers will warn if they see raw types. If you compile raw types with Java 5 onward, you will get warning like this:

ArrayList is a raw type. References to generic type ArrayList<E> should be parameterized

Happy Leaning !!

Read More:

Diamond operator in wiki

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Nguyen Tinh

    October 14, 2013

    Thanks

Comments are closed on this article!

Search Tutorials

Java 7 Tutorial

  • Java 7 – Feature Changes
  • Java 7 – Diamond Operator
  • Java 7 – String in Switch Statement
  • Java 7 – try-with-resources
  • Java 7 – Number Formatting
  • Java 7 – Suppressed Exceptions
  • Java 7 – Exception Handling
  • Java 7 – Fork/Join Framework
  • Java 7 – WatchService

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces