2

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?

15
  • return type of asList() is static List ? Commented Nov 8, 2012 at 11:25
  • 1
    I think your wanted to write new ArrayList<Integer>() on first line. Commented Nov 8, 2012 at 11:27
  • 1
    @miNde I think he uses Java 7 with diamond syntax. Commented Nov 8, 2012 at 11:30
  • 2
    @miNde: wrong (at least for Java 7). Using <> asks the compiler to fill in the type, if he can. Using nothing at all uses a raw type and is discouraged. Commented Nov 8, 2012 at 11:32
  • 1
    @rAkesH: you have posted two completely separate questions, please post them as separate posts. Commented Nov 8, 2012 at 11:33

6 Answers 6

4

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     }
Sign up to request clarification or add additional context in comments.

6 Comments

In my opinion it is important to say that it is not an instance of the well-known java.util.ArrayList which is returned by Arrays.asList(). It is an instance from java.util.Arrays.ArrayList which is a fixed-size implementation with private visibility modifier. Even better in my opinion is to not mention the used implementation at all.
@FabianBarney You're right. I edited. Thanks. I added the implementation to show there was a concrete class and that we weren't magically using the interface (I'm not really sure as how best to answer to the second question as I don't really get OP's problem, I wouldn't have answered if there weren't the first question).
Thank you for your answer.I know objects can exist only for concrete types.I am interested in from does the implementation come.Could u also please mention which class provides the implementation of Sort() method on Collection interface?
When you're doing Collections.sort(, you're not using the Collection interface but the Collections class.
I didn't observe it closely enough to see the difference of 1 letter.I was going to ask another stupid question that why don't ArrayList,HashSet ect have a Sort() method since Collection is the super interface of interfaces they implement but before that i noticed Collection interface doesn't have a Sort() method.Collections class happens to be contradiction to design by interfaces
|
0

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.

Comments

0

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.

Comments

0
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

Comments

0
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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.