I am creating some classes to represent Unsigned numbers for Java and deriving them from Number. One of my goals is to make them box and unbox primitive values but I can’t get this to work nor find any discussion of how to achieve this elsewhere.
Can this be done and if so how?
Here is my not working code!
public class Unsigned8 extends Number implements Comparable<Unsigned8>{
public static final long MAX_VALUE = 256L;
public static final long MIN_VALUE = 0;
private long _Value;
public Unsigned8(long value)
{
this._Value = value;
}
@Override
public byte byteValue() {
return (byte) _Value;
}
@Override
public short shortValue() {
return (short) _Value;
}
@Override
public int intValue() {
return (int) _Value;
}
@Override
public long longValue() {
return (long) _Value;
}
@Override
public double doubleValue() {
return _Value;
}
@Override
public float floatValue() {
return _Value;
}
@Override
public int compareTo(Unsigned8 target) {
return (int) (_Value - target._Value);
}
}
You can’t. What I do is use an Unsigned helper class for
shortintandlongso that your don’t incur the overhead of a wrapper to supportunsignedtypes. See the link for examples.