How get premtive variable data type in print statment . tell method that tell about data type of variable?
Asked
Active
Viewed 94 times
-4
-
1Please provide the print statement. – Chris311 Mar 15 '16 at 12:01
-
Duplicate: http://stackoverflow.com/questions/10438448/is-there-a-way-to-output-the-java-data-type-to-the-console – Ferit Mar 15 '16 at 12:03
4 Answers
1
If you are invoking a method like
System.out.printf("%d", 5);
then System.out.printf
doesn't actually receive a primitive argument: the variadic parameter is of type Object[]
, so the 5
will be autoboxed to Integer
in this case.
However, there is no token listed in the formatter syntax documentation which will print the parameter's type in the string.
You can write overloaded methods which are specialized for each of the primitive types:
String typeName(boolean _) { return "boolean"; }
String typeName(byte _) { return "byte"; }
String typeName(short _) { return "short"; }
String typeName(int _) { return "int"; }
String typeName(long _) { return "long"; }
String typeName(float _) { return "float"; }
String typeName(double _) { return "double"; }
String typeName(char _) { return "char"; }
You can then invoke this like:
int a = 5;
float f = 4.5f;
System.out.print(typeName(a)); // outputs "int"
System.out.print(typeName(f)); // outputs "float"

Andy Turner
- 137,514
- 11
- 162
- 243
0
You can print Wrapper Type, not primitive type. Hope this will help :
int i=5;
System.out.println(((Object)i).getClass().getName());

Rahman
- 3,755
- 3
- 26
- 43
-1
-
Assuming 'premtive' means 'primitive', this does nothing to help answer the question. – bradimus Mar 15 '16 at 12:09