Class JxnCast


  • public class JxnCast
    extends Object
    Collection of static methods for explicit primitive value cast in JXN. Also works for primitive arrays (see examples).

    Syntax: i4 = 12345; sh2 = JxnCast.toShort(i4)  instead of  (short)i4  in Java

    Jxn and Java implicitly only perform (automatic) casts which do not lead to overflow or (significant) loss of precision (truncation of fractional part). Performing an explicit cast the user must be aware that he/she accepts the possible overflow or loss of precision (see table below).

    JXN-Examples:
    
       b1 = JxnCast.toByte 1
        = 1 (byte)
       i8 = Long.MAX_VALUE
        = 9223372036854775807 (long)
       JxnCast.toInt i8
        = -1 (int)
       ! ^ overflow
       JxnCast.toFloat i8
        = 9.223372E18 (float)
       ! ^ 7 significant figures instead of 19 significant figures
       JxnCast.toDouble i8
        = 9.223372036854776E18 (double)
       ! ^ 16 significant figures instead of 19 significant figures
    
       ia = { 2, 3, 5 }
        = { 2, 3, 5 } (int[3])
       ba = JxnCast.toByte ia
        = { 2, 3, 5 } (byte[3])
       ca = JxnCast.toChar( 'a' + ia )
        = { c, d, f } (char[3])
       da = JxnCast.toDouble ca
        = { 99.0, 100.0, 102.0 } (double[3])
    
       da = { 1234567890., 123.456789, 12345654321. }
        = { 1.23456789E9, 123.456789, 1.2345654321E10 } (double[3])
       JxnCast.toInt da
        = { 1234567890, 123, 2147483647 } (int[3])
    
       ! cast to and from boolean in JXN extends Java
       JxnCast.toChar { true, false }
        = { 1, 0 } (char[2])
       JxnCast.toDouble { true, false }
        = { 1.0, 0.0 } (double[2]) 
       JxnCast.toBoolean { -0.001, 0., 0.001 }
        = { true, false, true } (boolean[3])
       JxnCast.toBoolean { '\#0', '1', '0', 't', 'f', 'Y', 'N' }
        = { false, true, false, true, false, true, false } (boolean[7])
     
     
    from  \  to double float long int short byte char boolean
    double  1) - P,O P,O P,O P,O P,O P,O P
    float e - P,O P,O P,O P,O P,O P
    long p p - O O O O P
    int e p e - O O O P
    short e e e e - O E P
    byte e e e e e - E P
    char e e e e E O - P
    boolean E E E E E E E -
     
    E:explicit cast, exact (reversible)     e:implicit cast, exact (reversible)     E:special implementation  2)
    P:explicit cast, possible loss of precision p:implicit cast, possible loss of precision P:special implementation  2)
    O:explicit cast, possible overflow p:implicit cast, insignificant loss of precision
     1)  As alternatives to cast from double consider  Math.round(double), Math.rint(double), Math.floor(double), Math.ceil(double)
     2)  Cast to and from boolean as defined in JXN extends the Java standard.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean toBoolean​(boolean value)
      Returns value (as boolean).
      static boolean toBoolean​(byte value)
      Returns true, if value is not 0.
      static boolean toBoolean​(char value)
      Returns true, if value is one of  '1' 'T' 't' 'Y' 'y'.
      static boolean toBoolean​(double value)
      Returns true, if value is not 0.0.
      static boolean toBoolean​(float value)
      Returns true, if value is not 0.0.
      static boolean toBoolean​(int value)
      Returns true, if value is not 0.
      static boolean toBoolean​(long value)
      Returns true, if value is not 0.
      static boolean toBoolean​(short value)
      Returns true, if value is not 0.
      static byte toByte​(boolean value)
      Returns 1 if value == true, 0 else.
      static byte toByte​(byte value)
      Returns value (as byte).
      static byte toByte​(char value)
      Returns (byte)value.
      static byte toByte​(double value)
      Returns (byte)value.
      static byte toByte​(float value)
      Returns (byte)value.
      static byte toByte​(int value)
      Returns (byte)value.
      static byte toByte​(long value)
      Returns (byte)value.
      static byte toByte​(short value)
      Returns (byte)value.
      static char toChar​(boolean value)
      Returns '1' if value == true, '0' else.
      static char toChar​(byte value)
      Returns (char)value.
      static char toChar​(char value)
      Returns value (as char).
      static char toChar​(double value)
      Returns (char)value.
      static char toChar​(float value)
      Returns (char)value.
      static char toChar​(int value)
      Returns (char)value.
      static char toChar​(long value)
      Returns (char)value.
      static char toChar​(short value)
      Returns (char)value.
      static double toDouble​(boolean value)
      Returns 1.0 if value == true, 0.0 else.
      static double toDouble​(byte value)
      Returns value (as double).
      static double toDouble​(char value)
      Returns value (as double).
      static double toDouble​(double value)
      Returns value (as double).
      static double toDouble​(float value)
      Returns value (as double).
      static double toDouble​(int value)
      Returns value (as double).
      static double toDouble​(long value)
      Returns value (as double).
      static double toDouble​(short value)
      Returns value (as double).
      static float toFloat​(boolean value)
      Returns 1.0f if value == true, 0.0f else.
      static float toFloat​(byte value)
      Returns value (as float).
      static float toFloat​(char value)
      Returns value (as float).
      static float toFloat​(double value)
      Returns (float)value.
      static float toFloat​(float value)
      Returns value (as float).
      static float toFloat​(int value)
      Returns value (as float).
      static float toFloat​(long value)
      Returns value (as float).
      static float toFloat​(short value)
      Returns value (as float).
      static int toInt​(boolean value)
      Returns 1 if value == true, 0 else.
      static int toInt​(byte value)
      Returns value (as int).
      static int toInt​(char value)
      Returns value (as int).
      static int toInt​(double value)
      Returns (int)value.
      static int toInt​(float value)
      Returns (int)value.
      static int toInt​(int value)
      Returns value (as int).
      static int toInt​(long value)
      Returns (int)value.
      static int toInt​(short value)
      Returns value (as int).
      static long toLong​(boolean value)
      Returns 1 if value == true, 0 else.
      static long toLong​(byte value)
      Returns value (as long).
      static long toLong​(char value)
      Returns value (as long).
      static long toLong​(double value)
      Returns (long)value.
      static long toLong​(float value)
      Returns (long)value.
      static long toLong​(int value)
      Returns value (as long).
      static long toLong​(long value)
      Returns value (as long).
      static long toLong​(short value)
      Returns value (as long).
      static short toShort​(boolean value)
      Returns 1 if value == true, 0 else.
      static short toShort​(byte value)
      Returns value (as short).
      static short toShort​(char value)
      Returns (short)value.
      static short toShort​(double value)
      Returns (short)value.
      static short toShort​(float value)
      Returns (short)value.
      static short toShort​(int value)
      Returns (short)value.
      static short toShort​(long value)
      Returns (short)value.
      static short toShort​(short value)
      Returns value (as short).
      static String toString​(boolean value)
      Returns value as String.
      static String toString​(byte value)
      Returns value as String.
      static String toString​(char value)
      Returns value as String.
      static String toString​(double value)
      Returns value as String.
      static String toString​(float value)
      Returns value as String.
      static String toString​(int value)
      Returns value as String.
      static String toString​(long value)
      Returns value as String.
      static String toString​(short value)
      Returns value as String.
    • Method Detail

      • toDouble

        public static double toDouble​(double value)
        Returns value (as double).
      • toDouble

        public static double toDouble​(float value)
        Returns value (as double). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
      • toDouble

        public static double toDouble​(long value)
        Returns value (as double). Upcast is done automatically (implicit cast) and always possible without overflow or significant loss of precision.
      • toDouble

        public static double toDouble​(int value)
        Returns value (as double). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
      • toDouble

        public static double toDouble​(short value)
        Returns value (as double). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
      • toDouble

        public static double toDouble​(byte value)
        Returns value (as double). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
      • toDouble

        public static double toDouble​(char value)
        Returns value (as double). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
      • toDouble

        public static double toDouble​(boolean value)
        Returns 1.0 if value == true, 0.0 else.
      • toFloat

        public static float toFloat​(double value)
        Returns (float)value. Explicit downcast must accept possible overflow or loss of precision.
      • toFloat

        public static float toFloat​(float value)
        Returns value (as float).
      • toFloat

        public static float toFloat​(long value)
        Returns value (as float). Upcast is done automatically (implicit cast) and always possible without overflow or significant loss of precision.
      • toFloat

        public static float toFloat​(int value)
        Returns value (as float). Upcast is done automatically (implicit cast) and always possible without overflow or significant loss of precision.
      • toFloat

        public static float toFloat​(short value)
        Returns value (as float). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
      • toFloat

        public static float toFloat​(byte value)
        Returns value (as float). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
      • toFloat

        public static float toFloat​(char value)
        Returns value (as float). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
      • toFloat

        public static float toFloat​(boolean value)
        Returns 1.0f if value == true, 0.0f else.
      • toLong

        public static long toLong​(double value)
        Returns (long)value. Explicit downcast must accept possible overflow or loss of precision.
      • toLong

        public static long toLong​(float value)
        Returns (long)value. Explicit downcast must accept possible overflow or loss of precision.
      • toLong

        public static long toLong​(long value)
        Returns value (as long).
      • toLong

        public static long toLong​(int value)
        Returns value (as long). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
      • toLong

        public static long toLong​(short value)
        Returns value (as long). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
      • toLong

        public static long toLong​(byte value)
        Returns value (as long). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
      • toLong

        public static long toLong​(char value)
        Returns value (as long). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
      • toLong

        public static long toLong​(boolean value)
        Returns 1 if value == true, 0 else.
      • toInt

        public static int toInt​(double value)
        Returns (int)value. Explicit downcast must accept possible overflow or loss of precision.
      • toInt

        public static int toInt​(float value)
        Returns (int)value. Explicit downcast must accept possible overflow or loss of precision.
      • toInt

        public static int toInt​(long value)
        Returns (int)value. Explicit downcast must accept possible overflow.
      • toInt

        public static int toInt​(int value)
        Returns value (as int).
      • toInt

        public static int toInt​(short value)
        Returns value (as int). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
      • toInt

        public static int toInt​(byte value)
        Returns value (as int). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
      • toInt

        public static int toInt​(char value)
        Returns value (as int). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
      • toInt

        public static int toInt​(boolean value)
        Returns 1 if value == true, 0 else.
      • toShort

        public static short toShort​(double value)
        Returns (short)value. Explicit downcast must accept possible overflow or loss of precision.
      • toShort

        public static short toShort​(float value)
        Returns (short)value. Explicit downcast must accept possible overflow or loss of precision.
      • toShort

        public static short toShort​(long value)
        Returns (short)value. Explicit downcast must accept possible overflow.
      • toShort

        public static short toShort​(int value)
        Returns (short)value. Explicit downcast must accept possible overflow.
      • toShort

        public static short toShort​(short value)
        Returns value (as short).
      • toShort

        public static short toShort​(byte value)
        Returns value (as short). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
      • toShort

        public static short toShort​(char value)
        Returns (short)value. Explicit cast required.
      • toShort

        public static short toShort​(boolean value)
        Returns 1 if value == true, 0 else.
      • toByte

        public static byte toByte​(double value)
        Returns (byte)value. Explicit downcast must accept possible overflow or loss of precision.
      • toByte

        public static byte toByte​(float value)
        Returns (byte)value. Explicit downcast must accept possible overflow or loss of precision.
      • toByte

        public static byte toByte​(long value)
        Returns (byte)value. Explicit downcast must accept possible overflow.
      • toByte

        public static byte toByte​(int value)
        Returns (byte)value. Explicit downcast must accept possible overflow.
      • toByte

        public static byte toByte​(short value)
        Returns (byte)value. Explicit downcast must accept possible overflow.
      • toByte

        public static byte toByte​(byte value)
        Returns value (as byte).
      • toByte

        public static byte toByte​(char value)
        Returns (byte)value. Explicit downcast must accept possible overflow.
      • toByte

        public static byte toByte​(boolean value)
        Returns 1 if value == true, 0 else.
      • toChar

        public static char toChar​(double value)
        Returns (char)value. Explicit downcast must accept possible overflow or loss of precision.
      • toChar

        public static char toChar​(float value)
        Returns (char)value. Explicit downcast must accept possible overflow or loss of precision.
      • toChar

        public static char toChar​(long value)
        Returns (char)value. Explicit downcast must accept possible overflow.
      • toChar

        public static char toChar​(int value)
        Returns (char)value. Explicit downcast must accept possible overflow.
      • toChar

        public static char toChar​(short value)
        Returns (char)value. Explicit cast required.
      • toChar

        public static char toChar​(byte value)
        Returns (char)value. Explicit cast required.
      • toChar

        public static char toChar​(char value)
        Returns value (as char).
      • toChar

        public static char toChar​(boolean value)
        Returns '1' if value == true, '0' else.
      • toBoolean

        public static boolean toBoolean​(double value)
        Returns true, if value is not 0.0.
      • toBoolean

        public static boolean toBoolean​(float value)
        Returns true, if value is not 0.0.
      • toBoolean

        public static boolean toBoolean​(long value)
        Returns true, if value is not 0.
      • toBoolean

        public static boolean toBoolean​(int value)
        Returns true, if value is not 0.
      • toBoolean

        public static boolean toBoolean​(short value)
        Returns true, if value is not 0.
      • toBoolean

        public static boolean toBoolean​(byte value)
        Returns true, if value is not 0.
      • toBoolean

        public static boolean toBoolean​(char value)
        Returns true, if value is one of  '1' 'T' 't' 'Y' 'y'.
      • toBoolean

        public static boolean toBoolean​(boolean value)
        Returns value (as boolean).
      • toString

        public static String toString​(double value)
        Returns value as String. Same as   "" + value .
      • toString

        public static String toString​(float value)
        Returns value as String. Same as   "" + value .
      • toString

        public static String toString​(long value)
        Returns value as String. Same as   "" + value .
      • toString

        public static String toString​(int value)
        Returns value as String. Same as   "" + value .
      • toString

        public static String toString​(short value)
        Returns value as String. Same as   "" + value .
      • toString

        public static String toString​(byte value)
        Returns value as String. Same as   "" + value .
      • toString

        public static String toString​(char value)
        Returns value as String. Same as   "" + value .
      • toString

        public static String toString​(boolean value)
        Returns value as String. Same as   "" + value .