convert java type object to string fail for some java types

31,755

Solution 1

If you want to get String representation of Object you can use fieldValue.toString() method. In case fieldValue can be primitive type (it wont have any methods) you can use String.valueOf(fieldValue).

Solution 2

String is an Object but all the Objects are not Strings.

The cast can be to its own class type or to one of its subclass or superclass types or interfaces.

There are some rules of Object type casting in Java id it's a primitive type you can do

String.valueOf(fieldValue)

Share:
31,755
Jean Tennie
Author by

Jean Tennie

Updated on July 09, 2022

Comments

  • Jean Tennie
    Jean Tennie almost 2 years

    Im using the following code to convert type object to string, fieldValue defined as is type object .

    keyl.put(fieldName, (String) fieldValue);
    

    the values of the type object can be only java types such as

    java decimal ,byte,float, calendar ,date etc ...
    

    when i got in fieldValue value of java.util.date I got an exception since the casting is not success. how can i overcome this issue?