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. Object object = Integer.valueOf(1234);
  13.  
  14. int result2 = (int)object; //works fine
  15. System.out.println("r2: " + result2);
  16. int result1 = int.class.cast(object); //throws ClassCastException: Cannot convert java.lang.integer to int
  17. System.out.println("r1: " + result1);
  18. }
  19. }
Runtime error #stdin #stdout #stderr 0.12s 320576KB
stdin
Standard input is empty
stdout
r2: 1234
stderr
Exception in thread "main" java.lang.ClassCastException: Cannot cast java.lang.Integer to int
	at java.lang.Class.cast(Class.java:3369)
	at Ideone.main(Main.java:16)