1

I'm reading about Java Generics. and I want to ask what is the difference between the following statements.

1: List<String> list = new ArrayList(3);

2: List<String> list = new ArrayList<String>(2);

3: List<String> list = new ArrayList<String>();

4a: List<String> list = new ArrayList("A"); // why I can't use String?

4b: List<String> list = new ArrayList('a'); // but char works fine.

I'm reading Java Docs on Generics and after that I need to ask the above questions because I didn't get exact answer.(May be due to poor English)

11
  • 6
    char isn't working fine, it is being treated as an int. a good place to start is reading the javadoc for ArrayList. Commented Sep 25, 2015 at 13:33
  • @SeanBright ArrayList can just take int as parameter? if yes then what is the working of that parameter? Commented Sep 25, 2015 at 13:35
  • the documentation i linked to explains that Commented Sep 25, 2015 at 13:35
  • 1
    @LetDoit Sean's link is the official Java documentation (called JavaDocs) - whatever it says here is how the language works. It goes into a lot of detail about method overloads, parameters, class hierarchy, etc... Your link, on the other hand, is part of the (official) tutorial, which is typically good enough for beginners but doesn't have as much detail as the documentation. Commented Sep 25, 2015 at 13:43
  • 1
    @LetDoit The JavaDocs are a bit dry to read if you're trying to learn programming in general. I think it's okay if you stick with the tutorials for now and go to the JavaDoc when you have more specific questions like this one. Commented Sep 25, 2015 at 13:48

2 Answers 2

6

This has nothing to do with generics. ArrayList has a constructor that takes an int, which represents the initial capacity of the List. A char ('a') is convertible to int, which is why 4b works just like 1 and 2. There's no constructor that takes a String, so 4a doesn't pass compilation.

EDIT:

Instantiating a class using a raw type (i.e. without a type parameter, as in List<String> list = new ArrayList(3)), is something you shouldn't do, as it is less type safe, and is only allowed for backwards compatibility.

Sign up to request clarification or add additional context in comments.

7 Comments

Well, that's explaining 4a... the difference between 1 and 2 does have do do with generics...
@Eran when I use String as type argument as in option 2 or 3. new ArrayList<String>(); then why Compiler notify me for Diamond operator? <> ?
@LetDoit The diamond operator is a shortcut that lets you type less code. Instead of writing List<String> list = new ArrayList<String>(2);, you can write the equivalent List<String> list = new ArrayList<>(2);.
@Eran ok the second as you says in your first line, which represents the initial capacity of the List. when I use List<String> list = new ArrayList<String>(1); and use list.add("a"); more than 2 time then why it's not show me index out of bound or extra adding error?
@Let Do it No, it can improve performance. If you know in advance that your list will contain at least 100000 elements, you can use 100000 as the initial capacity and save the time it would take the capacity to grow automatically as you add elements to the list.
|
0

The ArrayList constructor you're using has nothing to do with the generic type. It simply takes an int signifying its initial capacity.

When you call new ArrayList('a'), you're simply promoting 'a' to an int (65).

2 Comments

Why everyone repeating answer of 4th option?
same the Sean's link you also gave me javase7 link why not 8?

Your Answer

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