Pour object into check string if valid

Im using the following code that fieldValue can have a simple property, there is a way to check before im doing this code if fieldValue does not contain an object that cannot be passed to a string? To avoid a dump

keyVal.put(fieldName, (String) fieldValue); 
+5
source share
3 answers
 if (fieldValue instanceof String) 
+7
source

Since String is a final class (and therefore cannot have subclasses), I would consider using getClass over instanceof :

 if (fieldValue != null && fieldValue.getClass() == String.class) 
+2
source
 if (fieldValue instanceof String) { keyVal.put(fieldName, (String) fieldValue); } 
+1
source

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


All Articles