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 short
int
and long
so that your don't incur the overhead of a wrapper to support unsigned
types. See the link for examples.
No. You get a relation between primitives and its wrapper that follows:
Primitive type Wrapper class boolean Boolean byte Byte char Character float Float int Integer long Long short Short
They are limited to these types. Documentation here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With