I saw a piece of java code using Hashmap as follows:
Map indata = new HashMap(12);
//load data
indata.put(“checking”, ((object) new Double(1.0)));
indata.put(“PURPOSE”, ((object)"2”));
What confuse me is the two useages of "put" method, in which we have ((object) new Double(1.0))
and (object)"2"
. What do (object)
function here? Any differences between ((object) new Double(1.0))
and (object)"2"
?
It's a cast to Object
(note that it's capitalized in Java).
It seems to be pointless here, as the code will compile fine without the cast (since the non-generic version of Map
expects Object
as the type of both key and value, so passing a String
or a Double
does not require explicit casting).
Q: What do (object) function here?
A: Its just casting String
and Double
to Object.
Q: Any differences between ((object) new Double(1.0)) and (object)"2"?
A: NO
((object) new Double(1.0)) - casting Double into Object
((object)"2”) - casting String into Object
EDIT:
Just realized from comments that it should be Object
and not object
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.