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"?>

