title: Are List and List<String> the same in Groovy?
body: <p><strong>Question 1</strong></p>
  
  <p>Is it irrelevant whether a <code>List</code> (list of objects) or a <code>List&lt;String&gt;</code> (list of Strings) is used in Groovy? </p>
  
  <p>In the code example below, both lists end up being an <code>ArrayList</code> (ArrayList of objects). Would have expected the second list to be an <code>ArrayList&lt;String&gt;</code> (ArrayList of Strings).</p>
  
  <p>Does Groovy lose the type information when the class is compiled and infer it when the compiled class is executed?</p>
  
  <p><strong>Example 1</strong></p>
  
  <pre><code>List untypedList = ["a", "b", "c"]
  List&lt;String&gt; typedList = ["a", "b", "c"]
  
  println "Untyped list List:       ${untypedList.getClass()}"
  println "Typed list List&lt;String&gt;: ${typedList.getClass()}"
  </code></pre>
  
  <p><strong>Output 1</strong></p>
  
  <pre><code>Untyped list List:       class java.util.ArrayList
  Typed list List&lt;String&gt;: class java.util.ArrayList // Would have expected ArrayList&lt;String&gt;
  </code></pre>
  
  <p><strong>Question 2</strong></p>
  
  <p>I would have expected the line <code>typedList &lt;&lt; new Integer(1)</code> in the example below to fail with an exception because I'm trying to put an <code>int</code> into a list of Strings. Can anyone explain why I can add an <code>int</code> to the <code>String</code>-typed <code>List</code>?</p>
  
  <p>The output shows that it remains an <code>Integer</code>, i.e. it's not on-the-fly converted to a <code>String</code> "1".</p>
  
  <p><strong>Example 2</strong></p>
  
  <pre><code>List untypedList = ["a", "b", "c"]
  List&lt;String&gt; typedList = ["a", "b", "c"]
  
  untypedList &lt;&lt; new Integer(1)
  typedList &lt;&lt; new Integer(1) // Why does this work? Shouldn't an exception be thrown?
  
  println "Types:"
  println "Untyped list List:       ${untypedList.getClass()}"
  println "Typed list List&lt;String&gt;: ${typedList.getClass()}"
  
  println "List contents:"
  println untypedList
  println typedList
  
  println "Untyped list:"
  untypedList.each { println it.getClass() }
  println "Typed list:"
  typedList.each { println it.getClass() }
  </code></pre>
  
  <p><strong>Output 2</strong></p>
  
  <pre><code>Types:
  Untyped list List:       class java.util.ArrayList
  Typed list List&lt;String&gt;: class java.util.ArrayList
  List contents:
  [a, b, c, 1]
  [a, b, c, 1]
  Untyped list:
  class java.lang.String
  class java.lang.String
  class java.lang.String
  class java.lang.Integer
  Typed list:
  class java.lang.String
  class java.lang.String
  class java.lang.String
  class java.lang.Integer
  </code></pre>