Class JxnCast
- java.lang.Object
-
- 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 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)
Returnsvalue
(asboolean
).static boolean
toBoolean(byte value)
Returnstrue
, ifvalue
is not0
.static boolean
toBoolean(char value)
Returnstrue
, ifvalue
is one of'1' 'T' 't' 'Y' 'y'
.static boolean
toBoolean(double value)
Returnstrue
, ifvalue
is not0.0
.static boolean
toBoolean(float value)
Returnstrue
, ifvalue
is not0.0
.static boolean
toBoolean(int value)
Returnstrue
, ifvalue
is not0
.static boolean
toBoolean(long value)
Returnstrue
, ifvalue
is not0
.static boolean
toBoolean(short value)
Returnstrue
, ifvalue
is not0
.static byte
toByte(boolean value)
Returns1
ifvalue == true
,0
else.static byte
toByte(byte value)
Returnsvalue
(asbyte
).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'
ifvalue == true
,'0'
else.static char
toChar(byte value)
Returns(char)value
.static char
toChar(char value)
Returnsvalue
(aschar
).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)
Returns1.0
ifvalue == true
,0.0
else.static double
toDouble(byte value)
Returnsvalue
(asdouble
).static double
toDouble(char value)
Returnsvalue
(asdouble
).static double
toDouble(double value)
Returnsvalue
(asdouble
).static double
toDouble(float value)
Returnsvalue
(asdouble
).static double
toDouble(int value)
Returnsvalue
(asdouble
).static double
toDouble(long value)
Returnsvalue
(asdouble
).static double
toDouble(short value)
Returnsvalue
(asdouble
).static float
toFloat(boolean value)
Returns1.0f
ifvalue == true
,0.0f
else.static float
toFloat(byte value)
Returnsvalue
(asfloat
).static float
toFloat(char value)
Returnsvalue
(asfloat
).static float
toFloat(double value)
Returns(float)value
.static float
toFloat(float value)
Returnsvalue
(asfloat
).static float
toFloat(int value)
Returnsvalue
(asfloat
).static float
toFloat(long value)
Returnsvalue
(asfloat
).static float
toFloat(short value)
Returnsvalue
(asfloat
).static int
toInt(boolean value)
Returns1
ifvalue == true
,0
else.static int
toInt(byte value)
Returnsvalue
(asint
).static int
toInt(char value)
Returnsvalue
(asint
).static int
toInt(double value)
Returns(int)value
.static int
toInt(float value)
Returns(int)value
.static int
toInt(int value)
Returnsvalue
(asint
).static int
toInt(long value)
Returns(int)value
.static int
toInt(short value)
Returnsvalue
(asint
).static long
toLong(boolean value)
Returns1
ifvalue == true
,0
else.static long
toLong(byte value)
Returnsvalue
(aslong
).static long
toLong(char value)
Returnsvalue
(aslong
).static long
toLong(double value)
Returns(long)value
.static long
toLong(float value)
Returns(long)value
.static long
toLong(int value)
Returnsvalue
(aslong
).static long
toLong(long value)
Returnsvalue
(aslong
).static long
toLong(short value)
Returnsvalue
(aslong
).static short
toShort(boolean value)
Returns1
ifvalue == true
,0
else.static short
toShort(byte value)
Returnsvalue
(asshort
).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)
Returnsvalue
(asshort
).static String
toString(boolean value)
Returnsvalue
asString
.static String
toString(byte value)
Returnsvalue
asString
.static String
toString(char value)
Returnsvalue
asString
.static String
toString(double value)
Returnsvalue
asString
.static String
toString(float value)
Returnsvalue
asString
.static String
toString(int value)
Returnsvalue
asString
.static String
toString(long value)
Returnsvalue
asString
.static String
toString(short value)
Returnsvalue
asString
.
-
-
-
Method Detail
-
toDouble
public static double toDouble(double value)
Returnsvalue
(asdouble
).
-
toDouble
public static double toDouble(float value)
Returnsvalue
(asdouble
). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
-
toDouble
public static double toDouble(long value)
Returnsvalue
(asdouble
). Upcast is done automatically (implicit cast) and always possible without overflow or significant loss of precision.
-
toDouble
public static double toDouble(int value)
Returnsvalue
(asdouble
). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
-
toDouble
public static double toDouble(short value)
Returnsvalue
(asdouble
). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
-
toDouble
public static double toDouble(byte value)
Returnsvalue
(asdouble
). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
-
toDouble
public static double toDouble(char value)
Returnsvalue
(asdouble
). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
-
toDouble
public static double toDouble(boolean value)
Returns1.0
ifvalue == 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)
Returnsvalue
(asfloat
).
-
toFloat
public static float toFloat(long value)
Returnsvalue
(asfloat
). Upcast is done automatically (implicit cast) and always possible without overflow or significant loss of precision.
-
toFloat
public static float toFloat(int value)
Returnsvalue
(asfloat
). Upcast is done automatically (implicit cast) and always possible without overflow or significant loss of precision.
-
toFloat
public static float toFloat(short value)
Returnsvalue
(asfloat
). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
-
toFloat
public static float toFloat(byte value)
Returnsvalue
(asfloat
). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
-
toFloat
public static float toFloat(char value)
Returnsvalue
(asfloat
). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
-
toFloat
public static float toFloat(boolean value)
Returns1.0f
ifvalue == 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)
Returnsvalue
(aslong
).
-
toLong
public static long toLong(int value)
Returnsvalue
(aslong
). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
-
toLong
public static long toLong(short value)
Returnsvalue
(aslong
). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
-
toLong
public static long toLong(byte value)
Returnsvalue
(aslong
). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
-
toLong
public static long toLong(char value)
Returnsvalue
(aslong
). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
-
toLong
public static long toLong(boolean value)
Returns1
ifvalue == 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)
Returnsvalue
(asint
).
-
toInt
public static int toInt(short value)
Returnsvalue
(asint
). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
-
toInt
public static int toInt(byte value)
Returnsvalue
(asint
). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
-
toInt
public static int toInt(char value)
Returnsvalue
(asint
). Upcast is done automatically (implicit cast) and always possible without overflow or loss of precision.
-
toInt
public static int toInt(boolean value)
Returns1
ifvalue == 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)
Returnsvalue
(asshort
).
-
toShort
public static short toShort(byte value)
Returnsvalue
(asshort
). 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)
Returns1
ifvalue == 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)
Returnsvalue
(asbyte
).
-
toByte
public static byte toByte(char value)
Returns(byte)value
. Explicit downcast must accept possible overflow.
-
toByte
public static byte toByte(boolean value)
Returns1
ifvalue == 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)
Returnsvalue
(aschar
).
-
toChar
public static char toChar(boolean value)
Returns'1'
ifvalue == true
,'0'
else.
-
toBoolean
public static boolean toBoolean(double value)
Returnstrue
, ifvalue
is not0.0
.
-
toBoolean
public static boolean toBoolean(float value)
Returnstrue
, ifvalue
is not0.0
.
-
toBoolean
public static boolean toBoolean(long value)
Returnstrue
, ifvalue
is not0
.
-
toBoolean
public static boolean toBoolean(int value)
Returnstrue
, ifvalue
is not0
.
-
toBoolean
public static boolean toBoolean(short value)
Returnstrue
, ifvalue
is not0
.
-
toBoolean
public static boolean toBoolean(byte value)
Returnstrue
, ifvalue
is not0
.
-
toBoolean
public static boolean toBoolean(char value)
Returnstrue
, ifvalue
is one of'1' 'T' 't' 'Y' 'y'
.
-
toBoolean
public static boolean toBoolean(boolean value)
Returnsvalue
(asboolean
).
-
toString
public static String toString(double value)
-
toString
public static String toString(float value)
-
toString
public static String toString(long value)
-
toString
public static String toString(int value)
-
toString
public static String toString(short value)
-
toString
public static String toString(byte value)
-
toString
public static String toString(char value)
-
toString
public static String toString(boolean value)
-
-