Integer and inner duality?

Can someone explain this to me

 List<Integer> list = new LinkedList<Integer>();
 list.add(2);
 list.add(1);
 list.add(3);

when i used

 list.remove(1);

then the 1st element is deleted

 list.remove(new Integer("1"));

then the 2nd element is deleted.

So you can explain the behavior of automatic boxing and unboxing in the above scenario

when new A().a(new Integer("1"));

performed,

public class A {
    public void test(Integer i) {} //1
    public void test(int i) {} //2
    public void test(Object o) {}//3
} 

method 1 is executed

public class A {
    public void test(int i) {} //2
    public void test(Object o) {}//3
} 

method 3 adopted

+3
source share
8 answers

Basically, overload resolution will have a parameter Objectby parameter int, if an argument is presented Integer. (Prefers overloading with a parameter intabove Objector Integer, if represented by an argument int, of course.)

From the JLS section 15.12.2 (with discussion disabled):

(ยง15.12.2.1). .

- Java.

(ยง15.12.2.2) arity. - , .

(ยง15.12.2.3) , arity. - , .

(ยง15.12.2.4) arity, .

Integer Object, , unboxing.

, , , .

+2
 list.remove(1);

1 - , , 1,

 list.remove(new Integer("1"));

Integer (1),

+1

, Integer (2) , 2

remove (1) 1

remove ( Integer (2) Objecte, Integer (2) ( )

(1), Java , add (int) Outboxing.

, , , , ( ).

: Unboxing Integer , , .. Integer, Number Object, int .

0

. Integer (Integer), ( ).

Integer, test (int) test (Object). Object upcast , int unboxing, Object.

. , , .

0

Java:

(ยง15.12.2.2) ,

...

, , arity, / .

0
source

List provides methods:

public boolean remove(Object o)   // removes the first occurence of this object
public boolean remove(int i)      // removes object stored at index i

If we do list.remove(new Integer(1)), then the best match for this call is the first method (delete the object).

0
source

There are two delete functions in the list:

Although Java does autoboxing (and I believe that if the List.remove (int) method did not exist), the idea here is to determine how to invoke it. Since there is a valid operation with a primitive intthat will be called before any auto-communication occurs.

0
source

All Articles