I have a map of the type ConcurrentMap<String, Object>
for which I want to use getOrDefault
.
Now the returned value is type casted into BigDecimal
if key exists.
When I am using the below code, I get class cast exception.
final BigDecimal value= (BigDecimal) map.getOrDefault("key", 0);
I tried using (Object)0 but it makes no sense given everything in Java is an object. If getOrDefault
returns Object, then why is it throwing exception on casting 0 as Bigdecimal?
I used the below and it worked
final BigDecimal value= (BigDecimal) map.getOrDefault("key", BigDecimal.ZERO);
What's the reason that 0 cannot be casted?