Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Simple answer</h2> <p>Yes.</p> <h2>More detailed answer</h2> <p>You can either use generic collection, without <code>&lt;T&gt;</code> value, for example:</p> <pre><code>ArrayList a = new ArrayList(); a.add(2); a.add("String"); </code></pre> <p>Using collections without <code>&lt;T&gt;</code> is a bad habit and most IDEs / compilers give a warning here. You can circumvent it by using a collection of <code>Object</code>, i.e.:</p> <pre><code>ArrayList&lt;Object&gt; a = new ArrayList&lt;Object&gt;(); </code></pre> <p>Or you can find some common interface or supertype that these element must have in, for example <code>ArrayList&lt;Number&gt;</code> - and you can store various objects that have common <code>Number</code> superclass, i.e. BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, Short:</p> <pre><code>ArrayList&lt;Number&gt; a = new ArrayList&lt;Number&gt;(); a.add(2); // integer a.add(42L); // long a.add(123.45d); // double System.out.println(a.toString()); // =&gt; [2, 42, 123.45] </code></pre> <p>Note that it essentially means that <code>a</code> elements are of <code>Number</code> class — i.e. you can't ask to execute subclass-specific methods (for example, <a href="https://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#isInfinite()" rel="nofollow noreferrer">Double#isInfinite()</a>, which doesn't exist in <code>Number</code> superclass), although you can typecast in run-time if you somehow know it's safe to typecast:</p> <pre><code>a.get(2).isInfinite() // compile-time error ((Double) a.get(2)).isInfinite() // =&gt; false ((Double) a.get(1)).isInfinite() // run-time error (ClassCastException) </code></pre> <p>Run-time typecasting is also generally frowned upon, as it effectively circumvents proper compile-time type safety.</p> <p>Also note that it's impossible to assign (or use) <code>ArrayList&lt;Number&gt;</code> in place of <code>ArrayList&lt;Integer&gt;</code> and vice-versa, i.e. this will fail to compile:</p> <pre><code>public void printNumbers(ArrayList&lt;Number&gt; list) { list.forEach(System.out::println); } ArrayList&lt;Integer&gt; a = new ArrayList&lt;Integer&gt;(); printNumbers(a); // "incompatible types" </code></pre> <p>as well as this:</p> <pre><code>public void printIntegers(ArrayList&lt;Integer&gt; list) { list.forEach(System.out::println); } ArrayList&lt;Number&gt; a = new ArrayList&lt;Number&gt;(); printIntegers(a); // "incompatible types" </code></pre> <p>To declare a variable to be able to accept both <code>ArrayList&lt;Number&gt;</code> or any of its subclasses, one can use <code>ArrayList&lt;? extends Number&gt;</code> or <code>ArrayList&lt;? super Number&gt;</code> syntax. <code>extends</code> is generally used when you're going to consume (i.e. read) from the object in your method, <code>super</code> is used when you're going to produce (i.e. write). Given that printout is consuming, it's safe to use <code>extends</code>:</p> <pre><code>public void printNumbers(ArrayList&lt;? extends Number&gt; list) { list.forEach(System.out::println); } ArrayList&lt;Integer&gt; listInt = new ArrayList&lt;Integer&gt;(); printNumbers(listInt); // works ArrayList&lt;Double&gt; listDbl = new ArrayList&lt;Double&gt;(); printNumbers(listDbl); // also works </code></pre> <p>There is a good answer in <a href="https://stackoverflow.com/questions/4343202/difference-between-super-t-and-extends-t-in-java">Difference between &lt;? super T&gt; and &lt;? extends T&gt; in Java</a> for more in-depth explanation.</p>
 

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