How to parse a String variable into any data type in Java?

You could make this work for classes that have a string constructor like this: f.getType().getConstructor( String.class ).newInstance( value );


In Java, there is no universal method for converting a String into an instance of an arbitrary class. Many classes simply don't support such a conversion. And there's no standard interface for those that do support it.

Your best bet is to look for a constructor that accepts a String as its sole argument. Of course, not every class provides such a constructor, and there's no guarantee that the semantics would be what you'd expect.


There is a Github project (MIT Licensed) called type-parser which does converting a string value to the desired data type

Here is the project description from GitHub

This is a light weight library that does nothing but parse a string to a given type. Supports all applicable java classes, such as Integer, File, Enum, Float etc., including generic Collection types/interfaces such as List, Set, Map, arrays and even custom made types. Also possible to register your own parsers.