0

ArrayList declaration is modified from java versions one to another.what are the Advantage of the Modified ArrayList Declaration.

eg: Here I mentioned Three Types of Declaration from Various Version of Java Supported.I Set Java Compilance leve 1.4 in Eclipse

 ArrayList val=new ArrayList();//Supported at 1.4  
 ArrayList<String> val=new ArrayList<String>();//Syntax error, parameterized types are only available if source level is 1.5 or greater 
 ArrayList<String> val=new ArrayList< >();//operator is not allowed for source level below 1.7
Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52
Selvan
  • 35
  • 7

3 Answers3

5

It's just a process of continual improvement.

Originally, Java didn't have generics, and so you had to use the first form in your question. The ArrayList carried no information about what kind of objects it contained; they were just Object. A lot of casting was involved, e.g.:

ArrayList list = new ArrayList();
list.add(new Thing());
// ...
Thing t = (Thing)list.get(0);
// Blech--^^^^^^^

So Java got generics, and we could say what the ArrayList would contain1:

ArrayList<Thing> list = new ArrayList<Thing>();
list.add(new Thing());
// ...
Thing t = list.get(0);
//        ^--- No cast! :-)

But note the redundancy, I've had to write ArrayList<Thing> twice. (Well, really, I should have List<Thing> list = new ArrayList<Thing>();, but that's another issue.)

And so Java got the ability to infer the type parameter (<Thing>) from the variable declaration, which is where <> comes from:

ArrayList<Thing> list = new ArrayList<>();
// No need to repeat ----------------^^
list.add(new Thing());
// ...
Thing t = list.get(0);
//        ^--- No cast! :-)

In that example, it doesn't save a lot of typing, but consider a more complicated case, say a map mapping strings to lists of Thing:

Map<String, List<Thing>> map = new HashMap<>();

...is rather shorter / less redundant than

Map<String, List<Thing>> map = new HashMap<String, List<Thing>>();

1 The actual list still just works in terms of Object, it's just that the variable carries the type parameter information with it at compile-time (and a little bit at runtime) and the compiler inserts the casts for us; more in this answer. This was in order to provide backward-compatibility when generics were added to Java.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

Its all about L.H.S. reference variable which is used to hold the object reference.

things are convertible into generic or non-generic resolution during the compile time only.

Case 1:

ArrayList val=new ArrayList();//Supported at 1.4  

In this scenario val is a non-generic (not strict type) reference which hold a object ref of an ArrayList.

you can add any kind of object into this ref variable (val). it is not limited to String only. you may also add an Integer or Long as well.

Case 2:

ArrayList<String> val=new ArrayList<String>();//Syntax error,

In or After Java 1.5 (generic introduced) this declaration is allowed and ready to use. here val is a generic (Strict-type) reference which can hold one and only String type Objects you learn more here

case 3:

parameterized types are only available if source level is 1.5 or greater 
 ArrayList<String> val=new ArrayList< >();//operator is not allowed for source level below 1.7

In or After Java 1.7 (diamond operator) this declaration is allowed. its all about Left Hand Side ref. so, val can add only String type of object into.

For more clarification See

Community
  • 1
  • 1
Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52
0

As newer versions of Java are rolling out, you see these kind of improvements. For example, let's take your case.

  1. Originally Java didn't had Generics. They were later supported from version 5 onward. And that's why the first line

    ArrayList val=new ArrayList(); //Supported at 1.4  
    

    It only supported type Object.

  2. As Generics got supported, from version 5, you could now specify the type of parameter on which you wanted to work upon. So this was commonly used in version 1.5 and 1.6.

    ArrayList<Integer> val = new ArrayList<Integer>();
    ArrayList<String> val = new ArrayList<String>();
    ArrayList<UserDefinedObject> val = new ArrayList<UserDefinedObject>();
    ...
    
  3. From version 1.7 onward, Type Inference was introduced.

    Type inference is a Java compiler's ability to look at each method invocation and corresponding declaration to determine the type argument (or arguments) that make the invocation applicable.
    ...
    ...
    Generic Methods introduced you to type inference, which enables you to invoke a generic method as you would an ordinary method

    and so you could write

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

    In this case, a java compiler can infer the type parameters of a generic method call and you do not have to specify them.

However, since Java is backward compatible, you can use the code from previous version in later version, but not vice versa.

Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71