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.?

+3


source to share


6 answers


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
+4


source


Ok, you can do something like this:

Integer i = 1;
Float f = 2f;
Object[] obArr = new Object[]{i, f};
Float fReconstruct = (Float) obArr[2]

      



Not exactly primitive types, but the closest you can get!

0


source


Regarding your first question, for anyone who might be using the Number class .enter image description here

0


source


You can use an object to hold a reference to any of these types, but it will be "wrapped" (int will be held as an integer, longer than Long, etc.) ...

0


source


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>();

      

Here is the project .

0


source


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 .

-1


source







All Articles