Boxing/Unboxing applicable to one's own types?

The following compiles:

Long instance1= (long) 1;

Long instance2= (long) 2;

long primitive= instance1 * instance2;

This demonstrates auto-boxing/unboxing in Java 5, yes?

However, say I make the following class...

publicclass MyLong

extends Numberimplements Comparable<Long>

{

privatelong value;

publicfinal Class TYPE = Long.TYPE;

public MyLong(

long value

){

this.value = value;

}

public String toString(){

return String.valueOf(value);

}

publicbyte byteValue(){

return (byte)value;

}

publicshort shortValue(){

return (short)value;

}

publicint intValue(){

return (int)value;

}

publiclong longValue(){

return (long)value;

}

publicfloat floatValue(){

return (float)value;

}

publicdouble doubleValue(){

return (double)value;

}

publicint compareTo(

Long aLong

){

long thisVal = this.value;

long anotherVal = aLong.longValue();

return (thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1));

}

publicboolean equals(Object obj){

if (objinstanceof Long){

return value == ((Long)obj).longValue();

}

if (objinstanceof MyLong){

return value == ((MyLong)obj).longValue();

}

if (objinstanceof Integer){

return value == ((Integer)obj).longValue();

}

returnfalse;

}

}

The following doesn't compile...

MyLong instance1 = (long) 5;

//incompatible types...

MyLong instance1 =new MyLong(1);

MyLong instance2 =new MyLong(2);

MyLong instance3 = instance1 * instance2;

//* cannot be applied to...

So is there something else I need to implement in class MyLong in order to get it to box and unbox automatically, or is that a feature reserved for Java's own primitive wrappers only? I'm guessing that the answer to this question is that auto-boxing is applied to primitive types only... which raises two more questions for me (Just ignore the following if I'm guessing wrong here):

1. What I'm really trying to do in my own project is alias a basic type in one place so I can change it with little effort later. i.e.

Right now I have something like:

class MyAPIClass{

String accountID;

String userName;

intmoney;

//..etc...

}

and what I want is...

class MyAPIClass{

MyAPIUseriduserID;

MyAPIUsername userName;

MyAPIMoneymoney;

//..etc...

}

class MyAPIUseridextends String{/*etc...*/};

class MyAPIUsernameextends String{/*etc...*/};

class MyAPIMoneyextends Integer{/*etc...*/};

My purpose being that, this way, if I decide MyAPIClass.money should really be a 'long' or a 'String' or a 'class Whatever', I'm only making changes in one place, plus the places where I make fundamentally incompatible conversions.

Since I can't implement my own basic types, and I can't extend the ones provided (they're all final), how do I abstract myself away from a basic type without writing a lot of 'get() / set() / .value()' methods?

2. Seems to me that both string concatenation and autoboxing demonstrate applications of operator overloading. However, this functionality is apparently available only to the people who work at Sun, to be applied to their own classes only. I don't want to be be too combative here, but isn't that kind of like saying "We're smart enough to be using operator overloading, but everyone else isn't"?>

[7162 byte] By [dcaudella] at [2007-11-27 10:00:53]
# 1

You could define MyAPIMoney and the rest as interfaces rather than classes. Then MyAPIClass could be declared and implemented in terms of the interfaces. You are free later to implement and reimplement MyAPIMoney without changing MyAPIClass.

The point is that the interfaces describe the functionality of MyAPIMoney which is all that MyAPIClass depends on.

pbrockway2a at 2007-7-13 0:32:27 > top of Java-index,Java Essentials,New To Java...
# 2

Thanks for the idea. You're right, of couse. And as a general rule, it's better to use interfaces than classes anyway. In my example, I'm really abusing 'extends', because I don't want to extend the functionality of Integer or Double or String or whatever.

The reason why I'm suggesting doing things this way is because I want to inherit the operator functionality of the basic types. No matter what interface I make, I would not be able to apply '= 123', '*', '+', etc to it. The only classes that these operators can be applied to are the boxing classes of the primitive types. Apparently, even if you make a class that is functionally the same as a primitive-boxing class, the compiler won't box for you.

i.e. so far as I can tell, 'Integer blah = (int) 123' will work, but 'MyInteger blah = (int) 123' will never work, even if the content of MyInteger is exactly the same as Integer.

But perhaps I could phrase my question a little more simply:

How does one 'typedef' in Java?

dcaudella at 2007-7-13 0:32:27 > top of Java-index,Java Essentials,New To Java...