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 153363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:47:16+00:00 2026-05-11T09:47:16+00:00

When initialising an instance of a Generic class in Java is there any benefit

  • 0

When initialising an instance of a Generic class in Java is there any benefit to specifying the Type on both sides of the statement?

Or to put it another way, what’s the difference between these two valid statements:

ArrayList<String> test = new ArrayList<String>(); 

and:

ArrayList<String> test = new ArrayList(); 

(It seems second statement is not equivalent to:

ArrayList<String> test = new ArrayList<Object>(); 

as the third statement in invalid and causes an incompatible types compile error.)

  • 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. 2026-05-11T09:47:17+00:00Added an answer on May 11, 2026 at 9:47 am

    The second statement winds up being more or less equivalent to the first, but only because generics are erased at runtime. You’ll get an ‘unchecked conversion’ warning, which is why I don’t like it.

    A better way is to have a static generic method like this:

    public static <T> List<T> newList() {     return new ArrayList<T>(); } 

    and then do

    List<String> test = newList(); 

    This is what Google Collections does.

    (And you should almost always be declaring your lists as List, not as ArrayList. Makes it easy to switch the implementation later.)

    Edit: dribeas asked in the comments what the exact difference is between the two declarations, and why I said they are ‘more or less equivalent’. Because of type erasure, the only difference between them is the warning. Here’s a small piece of code comparing them:

    import java.util.*;  class GenericDeclarationTest {     public static void main(String[] args) {         List<String> list1 = new ArrayList<String>();         list1.add('');         String s1 = list1.get(0);         List<String> list2 = new ArrayList();         list2.add('');         String s2 = list2.get(0);     } } 

    And here’s the generated bytecode (as printed by javap -c GenericDeclarationTest):

    Compiled from 'GenericDeclarationTest.java' class GenericDeclarationTest extends java.lang.Object{ GenericDeclarationTest();   Code:    0:   aload_0    1:   invokespecial   #1; //Method java/lang/Object.'<init>':()V    4:   return  public static void main(java.lang.String[]);   Code:    0:   new     #2; //class java/util/ArrayList    3:   dup    4:   invokespecial   #3; //Method java/util/ArrayList.'<init>':()V    7:   astore_1    8:   aload_1    9:   ldc     #4; //String    11:  invokeinterface #5,  2; //InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z    16:  pop    17:  aload_1    18:  iconst_0    19:  invokeinterface #6,  2; //InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;    24:  checkcast       #7; //class java/lang/String    27:  astore_2    28:  new     #2; //class java/util/ArrayList    31:  dup    32:  invokespecial   #3; //Method java/util/ArrayList.'<init>':()V    35:  astore_3    36:  aload_3    37:  ldc     #4; //String    39:  invokeinterface #5,  2; //InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z    44:  pop    45:  aload_3    46:  iconst_0    47:  invokeinterface #6,  2; //InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;    52:  checkcast       #7; //class java/lang/String    55:  astore  4    57:  return  } 

    As you can see (if you have the patience), the two are identical.

    Incidentally, this may become easier in Java 7. There is a proposal in Project Coin for ‘Improved Type Inference for Generic Instance Creation’. If it makes the final cut, the syntax will be:

    List<String> test = new ArrayList<>(); // or Map<String, Object> test2 = new HashMap<>(); 

    Not too hard to type, is it?

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

Sidebar

Ask A Question

Stats

  • Questions 81k
  • Answers 81k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is a nice little gotcha. What is happening is… May 11, 2026 at 4:26 pm
  • Editorial Team
    Editorial Team added an answer You could use a ListBox, with a UniformGrid. That would… May 11, 2026 at 4:26 pm
  • Editorial Team
    Editorial Team added an answer The OS will hold socket references for a defined time… May 11, 2026 at 4:26 pm

Related Questions

I understand copy constructor is called on three instances When instantiating one object and
I wish to initialize an array of java methods in the child class, as
Not certain if this will get much response due to the newness of Windows
When I launch an SWT application (via an Eclipse launch profile), I receive the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.