Is there a simple way to get Class of a primitive number type from String?
For "42"
the method would return int.class
,
for "25.8"
it would return float.class
, etc...
I only need to check for number types, so the method does not have to be concerned about booleans and bytes.
I am asking, just in case anyone knows about a library method that can do this thing.
Is there a simple way to get Class of a primitive number type from String?
AFAIK ... No there isn't.
One reason is that many numeric strings can have multiple representational types; e.g. "42"
could represented as:
byte
, short
, int
or long
,float
or double
,BigInteger
or a BigDecimal
, or ...String
, StringBuilder
or StringBuffer
.And the flip side is that a number like "25.8"
probably doesn't have a precise representation in any of the primitive types or their wrappers.
If there is (often) not single correct answer, it is not surprising that there is no general solution provided by the Java SE libraries, or any of the commonly user 3rd-party libraries.