types - Datatypes in Java and their representation -
i have question primitive types in java (int, double, long, etc).
question one:
is there way in java have datatype, lets xtype (lossless type) can hold of primitive types? example of hypothetical usage:
int x = 10; double y = 9.5; xtype newtypex = x; xtype newtypey = y; int m = newtypex; question two:
is there away tell bits if number (primitive type) integer, or double, or float, etc?
you can use number class, super class numeric primitive wrapper classes, in first snippet:
int x = 10; double y = 9.5; number newtypex = x; number newtypey = y; the conversion between primitive types (int, double) , object type (number) possible through feature called autoboxing. however, line won't compile:
int m = newtypex; because cannot assign super type variable int. compiler doesn't know exact type of newtypex here (even if assigned int value earlier); cares, variable double.
for getting runtime type of number variable, can e.g. use getclass method:
system.out.println(newtypex.getclass()); system.out.println(newtypey.getclass()); when used example snippet, print out
class java.lang.integer class java.lang.double
Comments
Post a Comment