I can't figure out why i have an error on this piece of code. Any suggestion?
Thanks
public class HelloWorld {
public static void main(String[] args) {
int choice = 2;
choice.className().getName();
}
}
I can't figure out why i have an error on this piece of code. Any suggestion?
Thanks
public class HelloWorld {
public static void main(String[] args) {
int choice = 2;
choice.className().getName();
}
}
You Cannot invoke className() on primitive type
You can't, because primitives are not objects.
If the fully-qualified name of a class is available, it is possible to get the corresponding Class using the static method Class.forName(). This cannot be used for primitive types.
You can't determine primitive variable data type like that way. code should be like below.
public class HelloWorld {
public static void main(String[] args) {
int choice = 2;
String type = ((Object)choice).getClass().getName();
}
}
You can't because it is of primitive type, but you can use Integer instead and to check if it belongs to specific object you can use
Object temp = 1;
if(temp instanceof Integer){
System.out.println("Integer Object");
}