I have an app where a property file contains data in the following format
name=abc#$#String
age=23#$#Integer
When the value is casted, cast it to values after #$#. is it possible to do this without using a switch or if then else statement.
What I would like to do is have a way to convert the string String or Integer to objects and be able to cast the value without using an if then else block. Something of this sort
String[] tmp = props.getProperty("name").split("#$#");
String name = (tmp[1])tmp[0];
First of all, you can’t cast a String object to an Integer object; String is not a subclass of Integer. You can, however, convert a String literal into an object and cast something to it:
However, the above still applies. You won’t be able to cast “23”, a String, into an Integer.
It looks like you’re over thinking this a bit. This could be as simple as an if else statement, or perhaps a Factory method, which would eventually do the same thing anyways.
Also, I would assume that you know the object type before hand, as you’re not going to map name to an Integer in the first place. In which case, you might as well do what alexandros said.