Vistritium
Vistritium

Reputation: 26

Invoking overloaded method with less specific argument

java.util.Vector has methods: remove(int index) and remove(Object o)

I have:

vector<Integer> a;
int b=3;

I want:

Invoke method remove(Object o) with b variable. Writing a.remove(b) obviously invokes remove(int index)

Thanks in advance, Maciej

Upvotes: 0

Views: 91

Answers (1)

Mark Peters
Mark Peters

Reputation: 81074

a.remove(Integer.valueOf(b)); 

Should work. An Integer will be resolved as a reference type first, and match remove(Object), before autoboxing is considered to call remove(int).

From the Java Language Spec, 15.2.2:

Compile-Time Step 2: Determine Method Signature

The first phase (§15.12.2.2) performs overload resolution without permitting boxing or unboxing conversion, or the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the second phase.

The second phase (§15.12.2.3) performs overload resolution while allowing boxing and unboxing...

Upvotes: 4

Related Questions