fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. Boolean[] booleans = new Boolean[]{true, false, false};
  13. String[] strings = new String[]{"hello", "world"};
  14. Integer[] integers = new Integer[]{1, 2, 3, 4};
  15.  
  16. // When the above instances are cast to Object[], it all works fine
  17. Object[] booleansObj = (Object[])booleans;
  18. Object[] stringsObj = (Object[])strings;
  19. Object[] integersObj = (Object[])integers;
  20.  
  21. // When the above instances are cast to List<T>. String and Integer still
  22. // works, but not for Boolean
  23. List<String> stringsList = (List<String>)strings;
  24. List<Integer> integersList = (List<Integer>)integers;
  25.  
  26. // this will have Cannot cast from Boolean[] to List<Boolean> error
  27. List<Boolean> booleansList = (List<Boolean>)booleans;
  28. }
  29. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:23: error: incompatible types: String[] cannot be converted to List<String>
List<String> stringsList = (List<String>)strings;
                                         ^
Main.java:24: error: incompatible types: Integer[] cannot be converted to List<Integer>
List<Integer> integersList = (List<Integer>)integers;
                                            ^
Main.java:27: error: incompatible types: Boolean[] cannot be converted to List<Boolean>
List<Boolean> booleansList = (List<Boolean>)booleans;
                                            ^
3 errors
stdout
Standard output is empty