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. private static Object obj;
  10.  
  11. public static void main (String[] args) {
  12. obj = "teste";
  13. String st = to(String.class);
  14. System.out.println(st);
  15.  
  16. obj = 10;
  17. Integer it = to(Integer.class);
  18. System.out.println(it);
  19.  
  20. try {
  21. obj = true;
  22. Double dt = to(Double.class); // Erro!
  23. System.out.println(dt);
  24. } catch(ClassCastException cce) {
  25. System.out.println("Erro: double esperado, boolean encontrado!");
  26. }
  27. }
  28.  
  29. public static <E> E to(Class<E> classe) {
  30. return classe.cast(obj);
  31. }
  32. }
Success #stdin #stdout 0.06s 380224KB
stdin
Standard input is empty
stdout
teste
10
Erro: double esperado, boolean encontrado!