Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert java type object to string fail for some java types

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?

like image 254
Jean Tennie Avatar asked Mar 23 '23 12:03

Jean Tennie


2 Answers

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).

like image 112
Pshemo Avatar answered Apr 06 '23 08:04

Pshemo


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)

like image 44
Suresh Atta Avatar answered Apr 06 '23 08:04

Suresh Atta