Convert java type object to fail string for some java types

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

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

type object values ​​can only be for java types such as

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

when I got the fieldValue value of java.util.date , I got an exception because the casting was unsuccessful. how can i solve this problem?

+4
source share
2 answers

If you want to get the String representation of an Object, you can use the fieldValue.toString() method. In case fieldValue can be a primitive type (it does not have any methods), you can use String.valueOf(fieldValue) .

+5
source

A string is an object, but all objects are not strings.

A cast can refer to a native type of a class or to one of its subclasses or types or interfaces of a superclass.

There are several rules of type of objects in java id this is a primitive type that you can do

String.valueOf(fieldValue)

+2
source

Source: https://habr.com/ru/post/1487073/


All Articles