Eli Sadoff Eli Sadoff - 1 month ago 5
Java Question

Can I write a literal intializer in Java for a non-primitive type?

Java's literal initializers are mostly used for primitives (e.g.

int i = 2
and
double j = 4.2
), but there are certain non-primitive classes that also have literal initializers (e.g.
String a = "Hello"
or
Number z = 43
). I am looking to implement an initializer similar to that in a class that I wrote. I have a class
Numeric
that extends
Number
, that I would like to be able to initialize as
Numeric a = 43
. Is there any way that this can be done in Java, and if so, how?

Here is the part of the source code for
Numeric


public class Numeric extends Number {

private HashMap<Primitive, Number> values;
private Primitive origin;

public Numeric(byte value) {
values = new HashMap<>();
values.put(Primitive.BYTE, value);
values.put(Primitive.SHORT, (short) value);
values.put(Primitive.INT, (int) value);
values.put(Primitive.LONG, (long) value);
values.put(Primitive.FLOAT, (float) value);
values.put(Primitive.DOUBLE, (double) value);
origin = Primitive.BYTE;
}

public Numeric(short value) {
values = new HashMap<>();
values.put(Primitive.BYTE, (byte) value);
values.put(Primitive.SHORT, value);
values.put(Primitive.INT, (int) value);
values.put(Primitive.LONG, (long) value);
values.put(Primitive.FLOAT, (float) value);
values.put(Primitive.DOUBLE, (double) value);
origin = Primitive.SHORT;
}

public Numeric(int value) {
values = new HashMap<>();
values.put(Primitive.BYTE, (byte) value);
values.put(Primitive.SHORT, (short) value);
values.put(Primitive.INT, value);
values.put(Primitive.LONG, (long) value);
values.put(Primitive.FLOAT, (float) value);
values.put(Primitive.DOUBLE, (double) value);
origin = Primitive.INT;
}

public Numeric(long value) {
values = new HashMap<>();
values.put(Primitive.BYTE, (byte) value);
values.put(Primitive.SHORT, (short) value);
values.put(Primitive.INT, (int) value);
values.put(Primitive.LONG, value);
values.put(Primitive.FLOAT, (float) value);
values.put(Primitive.DOUBLE, (double) value);
origin = Primitive.LONG;
}

public Numeric(float value) {
values = new HashMap<>();
values.put(Primitive.BYTE, (byte) value);
values.put(Primitive.SHORT, (short) value);
values.put(Primitive.INT, (int) value);
values.put(Primitive.LONG, (long) value);
values.put(Primitive.FLOAT, value);
values.put(Primitive.DOUBLE, (double) value);
origin = Primitive.FLOAT;
}

public Numeric(double value) {
values = new HashMap<>();
values.put(Primitive.BYTE, (byte) value);
values.put(Primitive.SHORT, (short) value);
values.put(Primitive.INT, (int) value);
values.put(Primitive.LONG, (long) value);
values.put(Primitive.FLOAT, (float) value);
values.put(Primitive.DOUBLE, value);
origin = Primitive.DOUBLE;
}


Primitive
is just a basic
enum
with all the primitive types.

Answer

String literals as a special case in Java. Numbers don't really have literals - what you're seeing here is a primitive (such as 2) being autoboxed to its wrapper class (such as Integer). Since all the numeric wrappers extends Number, this assignments works (in the same way that Object o = 4; would be a legal statement).

You can not add your own literals to the language, though. The best you could do is to create some static function that produces a new instance of your class.