0

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"?

2
  • Both casts to object. While retrieval it makes difference, you need to re-cast first one as Double and second one as String. Commented Jun 25, 2012 at 17:32
  • There is no object in java. Commented Jun 25, 2012 at 17:33

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

5 Comments

the question is "for what?" ;)
@dantuch: I have updated my answer to show what OP asked and what I understood (and answered). Please correct me if I am making any wrong assumptions.
Yes, you have answered it without mistake, but that's not the point. When you see bad code, don't try to tell or guess what it does, but instead just simply point what's wrong with it. Here casting to Object is just plain stupid :)
Anyway, +1 for your effort :)
@dantuch: Now onward will try not to miss a bad code before looking for whats wrong with it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.