Email address:
Password:
SCJP/OCPJP 1.6 certification
Practice Exams and Training
SCJP Online Training » Discussions

Overloading method with Integer, Number, and Object parameters

why doNumber(2L) invoked doNumber(Object)rather it shouldinvoke doNumber(Integer) because wrapping will done in corresponding Integer object

When we have 'L' at the end of a value, it makes the value a long type literal. So when we say 2L, it is kind of similar to saying (long)2.


You are right about wrapping (which we call as 'auto-boxing'). However, a long value could only be auto-boxed to java.lang.Long - not to java.lang.Integer. That's why doNumber(Integer) wouldn't be able to accept it, and hence it falls back to the one that takes any object - doNumber(Object).


Fred

ExamLab © 2008 - 2024