Note that there are some explanatory texts on larger screens.

plurals
  1. PODifference between List, List<?>, List<T>, List<E>, and List<Object>
    primarykey
    data
    text
    <p>What are the differences between <code>List</code>, <code>List&lt;?&gt;</code>, <code>List&lt;T&gt;</code>, <code>List&lt;E&gt;</code>, and <code>List&lt;Object&gt;</code>? </p> <p>Now I do not blindly ask this question, so please don't close this thread. Let me first introduce the base code:</p> <pre><code>private static List&lt;String&gt; names = new ArrayList&lt;String&gt;(); static { names.add("Tom"); names.add("Peter"); names.add("Michael"); names.add("Johnson"); names.add("Vlissides"); } public static void test(List&lt;String&gt; set){ System.out.println(set); } public static void main(String args[]){ test(names); } </code></pre> <p>I do understand that:</p> <p>1.<code>List</code>: is a raw type, therefore not <code>typesafe</code>. It will only generate a runtime error when the casting is bad. We want a compile time error when the cast is bad. Not recommended to use.</p> <p>2.<code>List&lt;?&gt;</code>: is an unbounded wildcard. But not sure what this is for? So if I change the <code>test</code> method to</p> <pre><code>public static void test(List&lt;?&gt; set){ System.out.println(set); } </code></pre> <p>it still works good. <strong>If you can explain the usage of this, I would greatly appreciate it.</strong></p> <p><strong>EDIT</strong>: If I do this:</p> <pre><code>public static void test(List&lt;?&gt; set){ set.add(new Long(2)); //--&gt; Error set.add("2"); //--&gt; Error System.out.println(set); } </code></pre> <p>but if I change <code>test</code> to this:</p> <pre><code>public static void test(List&lt;String&gt; set){ set.add(new Long(2)); //--&gt; Error set.add("2"); //--&gt; Work System.out.println(set); } </code></pre> <p>3.<code>List&lt;T&gt;</code>: </p> <pre><code>public static void test(List&lt;T&gt; set){ //T cannot be resolved System.out.println(set); } </code></pre> <p>I guess I don't understand this syntax. I saw something like this, and it works:</p> <pre><code>public &lt;T&gt; T[] toArray(T[] a){ return a; } </code></pre> <p>Please explain this for me please? Sometimes, <strong>I see <code>&lt;T&gt;</code>, or <code>&lt;E&gt;</code>, or <code>&lt;U&gt;</code>, <code>&lt;T,E&gt;</code>. Are they all the same or do they represent something different?</strong></p> <p>4.<code>List&lt;Object&gt;</code></p> <pre><code>public static void test(List&lt;Object&gt; set){ System.out.println(set); } </code></pre> <p>Then I got the error <code>The method test(List&lt;Object&gt;) is not application for the argument List&lt;String&gt;</code> for the below code. I am confused. I thought <code>String</code> was a subset of <code>Object</code>?</p> <pre><code>public static void main(String args[]){ test(names); } </code></pre> <p><strong>EDIT:</strong> If I try this </p> <pre><code>test((List&lt;Object&gt;)names); </code></pre> <p>then I got <code>Cannot cast from List&lt;String&gt; to List&lt;Object&gt;</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload