What's the error in this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
1st Jan 2022, 3:58 PM
Mike
3 Answers
+ 2
Mike What when if condition will not execute? Then program will return nothing. Do this: static <T> String Class(T o){ if (o instanceof Integer) return "int"; return "xyz"; }
1st Jan 2022, 4:34 PM
A͢J
A͢J - avatar
+ 2
To see the result, you need to write a data output function: public class Program { static int i = 1; public static void main(String[] args) { System.out.println(Class(i)); } static <T> String Class(T o){ if (o instanceof Integer) return "int"; else return "No Integer"; } }
1st Jan 2022, 5:37 PM
Solo
Solo - avatar
0
You can create a simple, generic solution like this: public class TypeCheck { public static void main(String[] args){ Object o = new Object(); int i = 1; String s = "Hello world"; System.out.println(getClass(o)); System.out.println(getClass(i)); System.out.println(getClass(s)); } public static String getClass(Object o){ return o.getClass().getSimpleName(); } } Output: Object Integer String The code is based off of this answer here: https://stackoverflow.com/a/26507980/16846240
2nd Jan 2022, 8:35 PM
Erick White
Erick White - avatar