Discussion
What will be the output of the program?
import java.util.*;
public class Fragment18
{
static String lookup(Map<Integer, String> m, Object key)
{
return m.get(key);
}
public static void main(String[] args)
{
Map<Integer, String> ids = new HashMap<Integer, String>();
ids.put(7, "seven");
System.out.println(lookup(ids, 7) + " " + lookup(ids, 7L));
}
}
Answer: B
The key was stored as an Integer. Looking up 7 boxes to an Integer and matches, but 7L boxes to a Long, and Long.equals(Integer) is false whatever the numeric value, so the second lookup misses and returns null. Map.get accepts any Object, so nothing warns you at compile time.
No comment is present. Be the first to comment.
Loading…
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development