fork(3) 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. int intVal = 3;
  13. long longVal = 2;
  14. double doubleVal = 3.6d;
  15. float floatVal = 5.3f;
  16. Integer valInt = 5;
  17. Long valLong = 68l;
  18. Double valDouble = 23.5;
  19. Float valFloat = 34.7f;
  20. String valString = "Teste";
  21. MyClass valMyClass = new MyClass();
  22. Object valObj = new Object();
  23. char charVal = 222; /* adicionado conforme comentário de @Piovezan */
  24. Character valChar = 222; /* adicionado conforme comentário de @Piovezan */
  25. boolean booleanVal = true; /* adicionado conforme comentário de @Piovezan */
  26. Boolean valBoolean = false; /* adicionado conforme comentário de @Piovezan */
  27. printIsNumber(intVal);
  28. printIsNumber(longVal);
  29. printIsNumber(doubleVal);
  30. printIsNumber(floatVal);
  31. printIsNumber(valInt);
  32. printIsNumber(valLong);
  33. printIsNumber(valDouble);
  34. printIsNumber(valFloat);
  35. printIsNumber(valString);
  36. printIsNumber(valMyClass);
  37. printIsNumber(valObj);
  38. printIsNumber(charVal); /* adicionado conforme comentário de @Piovezan */
  39. printIsNumber(valChar); /* adicionado conforme comentário de @Piovezan */
  40. printIsNumber(booleanVal); /* adicionado conforme comentário de @Piovezan */
  41. printIsNumber(valBoolean); /* adicionado conforme comentário de @Piovezan */
  42. }
  43.  
  44. public static void printIsNumber(Object value){
  45. System.out.println(value.getClass() + ", val: " + value + " is number: " + isNumber(value));
  46. }
  47.  
  48. public static boolean isNumber(Object value){
  49. return (value instanceof Number /* adicionado conforme comentário de @Piovezan */|| value instanceof Character);
  50. }
  51.  
  52. public static class MyClass{
  53.  
  54. }
  55. }
Success #stdin #stdout 0.1s 320576KB
stdin
Standard input is empty
stdout
class java.lang.Integer, val: 3 is number: true
class java.lang.Long, val: 2 is number: true
class java.lang.Double, val: 3.6 is number: true
class java.lang.Float, val: 5.3 is number: true
class java.lang.Integer, val: 5 is number: true
class java.lang.Long, val: 68 is number: true
class java.lang.Double, val: 23.5 is number: true
class java.lang.Float, val: 34.7 is number: true
class java.lang.String, val: Teste is number: false
class Ideone$MyClass, val: Ideone$MyClass@52e922 is number: false
class java.lang.Object, val: java.lang.Object@25154f is number: false
class java.lang.Character, val: Þ is number: true
class java.lang.Character, val: Þ is number: true
class java.lang.Boolean, val: true is number: false
class java.lang.Boolean, val: false is number: false