Java data types and their representations
I have a question about the primitive types in the Java ( int
, double
, long
etc.).
Question one:
Is there a way in Java to have a datatype, say XType
(Lossless type), that can contain any of the primitive types? An example of hypothetical use:
int x = 10;
double y = 9.5;
XType newTypeX = x;
XType newTypeY = y;
int m = newTypeX;
Second question:
Is it possible to specify from bits if this number (primitive type) is an integer, or double, or float, etc.?
source to share
You can use a class Number
that is the superclass of all numeric primitive wrapper classes in your first snippet:
int x = 10;
double y = 9.5;
Number newTypeX = x;
Number newTypeY = y;
Conversion between primitive types ( int
, double
) and object type ( Number
) is possible through a function called autoboxing . However, this line will not compile:
int m = newTypeX;
because you cannot assign a super type variable in int
. The compiler does not know the exact type newTypeX
here (even if it was previously assigned a value int
); for whatever he cares about, the variable can also be double
.
To get the runtime type of a variable, Number
you can, for example, use the method getClass
:
System.out.println(newTypeX.getClass());
System.out.println(newTypeY.getClass());
When used with the example snippet, this will print
class java.lang.Integer class java.lang.Double
source to share
Not yet (java 8), not the way you want to return a primitive value. But if, instead of a primitive type, you keep a primitive type wrapper (which are classes that descend from the class Object
and terminate their respective primitive, i.e. Byte
wraps Byte
, Integer
wraps int
, Long
wraps Long
, etc.), you can do this:
public class Holder<T extends Number> {
private final T number;
public Holder(T number) {
this.number = number;
}
public T get() {
return this.number;
}
}
You can use this class Holder
to store the instance of any subclass Number
, including primitives primitives . For example:
Holder<Integer> holderX = new Holder<>(5);
Holder<Double> holderY = new Holder<>(1.234);
int x = holderX.get(); // 5
double y = holderY.get(); // 1.234
The above is possible because of a Java feature called Generics and also another feature called autoboxing .
Extending generics to support primitive types is discussed for Java 9 or 10 (not sure) under the term "Specialization". In theory, this will allow you to:
List<int> list = new ArrayList<int>();
source to share
Generics is a generic programming tool that was added to the Java programming language in 2004 in J2SE 5.0. They allow "a type or method to work on objects of different types while enforcing compilation type safety."
For example, I like the one on the tutorialspoint:
public class GenericMethodTest
{
// generic method printArray
public static < E > void printArray( E[] inputArray )
{
// Display array elements
for ( E element : inputArray ){
System.out.printf( "%s ", element );
}
System.out.println();
}
public static void main( String args[] )
{
// Create arrays of Integer, Double and Character
Integer[] intArray = { 1, 2, 3, 4, 5 };
Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 };
Character[] charArray = { 'H', 'E', 'L', 'L', 'O' };
System.out.println( "Array integerArray contains:" );
printArray( intArray ); // pass an Integer array
System.out.println( "\nArray doubleArray contains:" );
printArray( doubleArray ); // pass a Double array
System.out.println( "\nArray characterArray contains:" );
printArray( charArray ); // pass a Character array
}
}
This will produce the following output:
Array integerArray contains:
1 2 3 4 5 6
Array doubleArray contains:
1.1 2.2 3.3 4.4
Array characterArray contains:
H E L L O Array integerArray contains:
1 2 3 4 5 6
Array doubleArray contains:
1.1 2.2 3.3 4.4
Array characterArray contains:
H E L L O
Read more about this link .
source to share