Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
2  
If fieldValue is Object type then maybe instead of casting use fieldValue.toString(), or if it can be primitive type then String.valueOf(fieldValue). –  Pshemo Jun 19 '13 at 15:36
    
If fieldValue is possibly null then String.valueOf(fieldValue) will prevent NullPointerException –  selig Jun 19 '13 at 15:38
    
@Pshemo Exactly :). OP gets an efficient answer,If he clarifies what is field-value. –  sᴜʀᴇsʜ ᴀᴛᴛᴀ Jun 19 '13 at 15:42
    
@Baadshah true, that is why I only post comments :) –  Pshemo Jun 19 '13 at 15:44
    
@Pshemo lol..got it ;). –  sᴜʀᴇsʜ ᴀᴛᴛᴀ Jun 19 '13 at 15:45
show 1 more comment

2 Answers

up vote 3 down vote accepted

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

share|improve this answer
2  
+1 for exact guess :) –  sᴜʀᴇsʜ ᴀᴛᴛᴀ Jun 19 '13 at 15:51
add comment

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|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.