Skip to main content

Extended auto(un)boxing of primitive types

3 replies [Last post]
vikstar
Offline
Joined: 2004-04-23
Points: 0

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 />
public interface Point {<br />
  public void setX(Number x);<br />
  public void setY(Number y);<br />
}</p>
<p>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 />
}</p>
<p>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 arguemtns without actually autoboxing them.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
slava
Offline
Joined: 2003-06-10
Points: 0

Autoboxing was a mistake. If you want consistent semantics for objects and primitive types, use a modern language like Lisp or Smalltalk. Sun should not have tried to smooth over the primitive/object distinction in the most crappy way imaginable.

tsinger
Offline
Joined: 2003-06-10
Points: 0

-1

[b]NO[/b], no more auto(un)boxing! It's the worst "feature", that was introduced in Java 5, because it might introduce subtle bugs (NullPointerExceptions).

Tom

vikstar
Offline
Joined: 2004-04-23
Points: 0

Sorry, i accidently must have pressed post message instead of preview. Please see the latest thread instead.