fork 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. Integer i1=1; // upper case Integer (not int), so it is an object
  13. Object o1=i1; // setting Object to any object is okay
  14. //Integer i2=o1; // this simply will not compile
  15. Integer i2=(Integer)o1; // this will work fine
  16. if(o1 instanceof Integer)
  17. System.out.println("o1 is Integer: "+(Integer)o1*2);
  18. if(o1 instanceof Double)
  19. System.out.println("o1 is Double: "+(Double)o1*2);
  20. System.out.println("passed the ifs");
  21. Double d1=(Double)o1; // this will result in ClassCastException in runtime
  22. }
  23. }
Runtime error #stdin #stdout #stderr 0.09s 33884KB
stdin
Standard input is empty
stdout
o1 is Integer: 2
passed the ifs
stderr
Exception in thread "main" java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Double (java.lang.Integer and java.lang.Double are in module java.base of loader 'bootstrap')
	at Ideone.main(Main.java:21)