How to create my own datatype in java, that will accept value range from 0 to 1024.
My declaration would be.
kilobyte a=10;
Is it possible?
How to create my own datatype in java, that will accept value range from 0 to 1024.
My declaration would be.
kilobyte a=10;
Is it possible?
No. Java doesn't allow that (i.e. creating a primitive). But you could create your own class for that.
You cannot create your own primitive, but you could have a wrapper Object
for that.
Dirty example here:
// note that class name doesn't follow Java conventions here
public class kilobyte {
private int number;
public kilobyte(Number number) {
if (number == null) {
throw new IllegalArgumentException("argument is null");
}
else if (number.intValue() > 1024 || number.intValue() < 0) {
throw new IllegalArgumentException("argument not within 0 < x < 1024 range");
}
else {
this.number = number.intValue();
}
}
// TODO getter/setter
}
No, you cant create your own primitive type neither overload the operators, so this is not possible in java.