0
What's the error in this code?
4 odpowiedzi
+ 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";
}
+ 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";
}
}
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