Skip to main content

Extended auto(un)boxing of primitive types

1 reply [Last post]
Joined: 2004-04-23

Sometimes auto(un)boxing occurs only for the purpose of passing an argument through a method. The following is a trivial (uncompilable) example to demostrate what I mean:

<br />
/* in a file*/<br />
public interface Point {<br />
  public void setX(Number x);<br />
  public void setY(Number y);<br />
}<br />
/* in a file */<br />
public class DoublePoint implements Point {<br />
  private double x, y;<br />
  public void setX(Number x) {<br />
    this.x = x;<br />
  }<br />
  public void setY(Number y) {<br />
    this.y = y;<br />
  }<br />
}<br />
/* somewhere in a method */<br />
Point p = new DoublePoint();<br />
p.setX(1.3);<br />
p.setY(2.7);<br />

I propose two things:

1) That java can auto(un)box to and from a Number.

2) For the compiler to recognize that auto(un)boxing is not actually required in this case, it is only used to satisfy the interface. So, it should simply pass the primitive values 1.3 and 2.7 through the methods to the x and y arguments without actually autoboxing them, as if setX(double) and setY(double) methods existed.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2004-04-23