Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3341232
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:41:57+00:00 2026-05-18T00:41:57+00:00

The diamond operator in java 7 allows code like the following: List<String> list =

  • 0

The diamond operator in java 7 allows code like the following:

List<String> list = new LinkedList<>();

However in Java 5/6, I can simply write:

List<String> list = new LinkedList();

My understanding of type erasure is that these are exactly the same. (The generic gets removed at runtime anyway).

Why bother with the diamond at all? What new functionality / type safety does it allow? If it doesn’t yield any new functionality why do they mention it as a feature? Is my understanding of this concept flawed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T00:41:57+00:00Added an answer on May 18, 2026 at 12:41 am

    The issue with

    List<String> list = new LinkedList();
    

    is that on the left hand side, you are using the generic type List<String> where on the right side you are using the raw type LinkedList. Raw types in Java effectively only exist for compatibility with pre-generics code and should never be used in new code unless
    you absolutely have to.

    Now, if Java had generics from the beginning and didn’t have types, such as LinkedList, that were originally created before it had generics, it probably could have made it so that the constructor for a generic type automatically infers its type parameters from the left-hand side of the assignment if possible. But it didn’t, and it must treat raw types and generic types differently for backwards compatibility. That leaves them needing to make a slightly different, but equally convenient, way of declaring a new instance of a generic object without having to repeat its type parameters… the diamond operator.

    As far as your original example of List<String> list = new LinkedList(), the compiler generates a warning for that assignment because it must. Consider this:

    List<String> strings = ... // some list that contains some strings
    
    // Totally legal since you used the raw type and lost all type checking!
    List<Integer> integers = new LinkedList(strings);
    

    Generics exist to provide compile-time protection against doing the wrong thing. In the above example, using the raw type means you don’t get this protection and will get an error at runtime. This is why you should not use raw types.

    // Not legal since the right side is actually generic!
    List<Integer> integers = new LinkedList<>(strings);
    

    The diamond operator, however, allows the right hand side of the assignment to be defined as a true generic instance with the same type parameters as the left side… without having to type those parameters again. It allows you to keep the safety of generics with almost the same effort as using the raw type.

    I think the key thing to understand is that raw types (with no <>) cannot be treated the same as generic types. When you declare a raw type, you get none of the benefits and type checking of generics. You also have to keep in mind that generics are a general purpose part of the Java language… they don’t just apply to the no-arg constructors of Collections!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How I can draw a Pascal's triangle in PASCAL Programming like a diamond from
I'm trying to write the Diamond-Square algorithm in Java to generate a random map
I have the following code: // button sets var sets = ['.diamond-colour','.diamond-cut','.diamond-clarity','.diamond-certificate']; // for
How can I directly pass a value from the diamond operator to a function
I rather like the diamond syntax for generics that Java 7 introduces - not
This code does not compile. public class Diamond { public static void diamondOfAsterisks(String *
The issue I have can be found by running the following code in Strawberry
I am wondering why the second map declaration (using the diamond operator) does not
There is my code: data Suit = Spade | Heart | Diamond | Club
I want to create a form (flash or java/ajax) that allows selection data to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.