Reputation: 1266
What is reason behind supporting the syntax below on java 1.7
List<Integer> ints=new ArrayList<>();
What flexibility does it provide? If we wanted ints to be of a different type we would explicitly cast it and it would throw an error if the conversion could not be performed.
I am also not clear on how exactly does this work.Since List is an interface how are we able to call a method on it
List<Integer> ints = Arrays.asList(1,2);
ints.get(1);
Since return type of asList()
is static<T> List<T>
it's okay to access a field of a static interface without any class providing an implementation of it.But how are we able to access a method on such an interface
The collections library has a lot of them like Collections.Sort()
Who provides the implementation of these methods?
Upvotes: 2
Views: 571
Reputation: 382122
Question 1
Using
List<Integer> ints=new ArrayList<>();
instead of
List<Integer> ints=new ArrayList<Integer>();
doesn't add any flexibility as they're exactly equivalent. The diamond (<>
) indicates that we don't want the raw type but the obvious parameterized type.
Oracle describes it here :
In Java SE 7, you can substitute the parameterized type of the constructor with an empty set of type parameters (<>):
It just makes the code a little less verbose, thus easier to read and maintain. This is probably more evident on Oracle's example :
Map<String, List<String>> myMap = new HashMap<>();
compared to
Map<String, List<String>> myMap = new HashMap<String, List<String>>();
Question 2
Arrays.asList(1,2);
returns an object from a concrete class implementing the List interface.
In Java 1.6, it's an instance of the fixed-size java.util.Arrays.ArrayList
:
3354 public static <T> List<T> asList(T... a) {
3355 return new ArrayList<T>(a);
3356 }
but the important point is that an object always is of a concrete class implementing the methods its interfaces defines.
When you're doing Collections.sort(
, you're not using the Collection
interface but the Collections class which contains this very concrete code :
132 public static <T extends Comparable<? super T>> void sort(List<T> list) {
133 Object[] a = list.toArray();
134 Arrays.sort(a);
135 ListIterator<T> i = list.listIterator();
136 for (int j=0; j<a.length; j++) {
137 i.next();
138 i.set((T)a[j]);
139 }
140 }
Upvotes: 4
Reputation: 9159
What flexibility does it provide?
It does not provide any flexibility; is just a syntactic sugaring. Kind of pointless since modern IDEs code completion already do that.
Since return type of asList() is static List it's okay to access a field of a static interface without any class providing an implementation of it.But how are we able to access a method on such an interface The collections library has a lot of them like Collections.Sort() Who provides the implementation of these methods?
The return type of asList()
is List; the type has nothing to do with the fact that the method is static. Arrays
is a class, not an interface, and asList() method is implemented there; the JDK provides the implementation.
Upvotes: 0
Reputation: 9357
List<Integer> ints=new ArrayList<>();
is exactly the same as writing
List<Integer> ints=new ArrayList<Integer>();
It doesn't add flexibility, it just let you keep generics with your ArrayList
whithout having to retype the type of the ArrayList
, and so the ArrayList
will not use raw type.
Some other thing is that writing this, you only get directly access to the List
interface methods on your ArrayList
object, event if these methods will have the behavior that is defined in the ArrayList
class.
Upvotes: 0
Reputation: 11298
List<Integer> ints=new ArrayList<>();
Its Generics easy to understand and Iterate
for(Integer intObj : ints ) {}
instead of
Iterator itr = ints.iterator() ;
while(itr.hasNext()) {
Integer intObj = (Integer) itr.next();
}
Since return type of Arrays.asList()
is static?
Yes java.util.Arrays
is an util class provides lots of utility methods.
But how are we able to access a method on such an interface?
Just view source code of ArrayList
there you can find implementation methods from List
interface.
Who provides the implementation of these methods?
The one who implement the interface like List
Set
etc
Upvotes: 0
Reputation: 1453
These are compile time checks, and are designed to ensure consistency, so implementations don't have to 'do' anything specific so long as they follow the rules laid down by the generics notation (e.g. for a method called with an object of type T, you only return something also of the same type T).
As has been pointed out, the diamond notation was brought in when it became clear that specifying a type in a constructor was unnecessarily verbose - the type can be clearly inferred (both by anyone reading the code, and by the compiler) from the target of the constructor.
Upvotes: 0
Reputation: 4995
You have a number of questions in your question.
Generics provide compile-time checking whereas casting errors are not discovered until runtime.
Your ints is an object that supports the List interface so it is valid to call methods on it.
Upvotes: 0