Java Utililty Methods Primitive Type Create

List of utility methods to do Primitive Type Create

Description

The list of methods to do Primitive Type Create are organized into topic(s).

Method

ObjecttoPrimitiveArray(Object[] array)
Convert an array of objects to an array of primitive types.
if (array instanceof Integer[]) {
    int[] r = new int[array.length];
    int i;
    int k = array.length;
    for (i = 0; i < k; i++)
        r[i] = (((Integer) array[i]) == null) ? 0 : ((Integer) array[i]).intValue();
    return r;
if (array instanceof Boolean[]) {
    boolean[] r = new boolean[array.length];
    int i;
    int k = array.length;
    for (i = 0; i < k; i++)
        r[i] = (((Boolean) array[i]) == null) ? false : ((Boolean) array[i]).booleanValue();
    return r;
if (array instanceof Byte[]) {
    byte[] r = new byte[array.length];
    int i;
    int k = array.length;
    for (i = 0; i < k; i++)
        r[i] = (((Byte) array[i]) == null) ? 0 : ((Byte) array[i]).byteValue();
    return r;
if (array instanceof Character[]) {
    char[] r = new char[array.length];
    int i;
    int k = array.length;
    for (i = 0; i < k; i++)
        r[i] = (((Character) array[i]) == null) ? 0 : ((Character) array[i]).charValue();
    return r;
if (array instanceof Double[]) {
    double[] r = new double[array.length];
    int i;
    int k = array.length;
    for (i = 0; i < k; i++)
        r[i] = (((Double) array[i]) == null) ? 0 : ((Double) array[i]).doubleValue();
    return r;
if (array instanceof Float[]) {
    float[] r = new float[array.length];
    int i;
    int k = array.length;
    for (i = 0; i < k; i++)
        r[i] = (((Float) array[i]) == null) ? 0 : ((Float) array[i]).floatValue();
    return r;
if (array instanceof Long[]) {
    long[] r = new long[array.length];
    int i;
    int k = array.length;
    for (i = 0; i < k; i++)
        r[i] = (((Long) array[i]) == null) ? 0 : ((Long) array[i]).longValue();
    return r;
if (array instanceof Short[]) {
    short[] r = new short[array.length];
    int i;
    int k = array.length;
    for (i = 0; i < k; i++)
        r[i] = (((Short) array[i]) == null) ? 0 : ((Short) array[i]).shortValue();
    return r;
throw new IllegalArgumentException();
ClasstoPrimitiveArrayType(Class c)
to Primitive Array Type
if (!c.isPrimitive())
    throw new RuntimeException(c + " is not a primitive type");
if (c == int.class)
    return int[].class;
if (c == long.class)
    return long[].class;
if (c == double.class)
    return double[].class;
...
booleantoPrimitiveBoolean(Object o)
to Primitive Boolean
Boolean b = toBoolean(o);
if (b != null) {
    return b.booleanValue();
return false;
booleantoPrimitiveBoolean(Object o)
to Primitive Boolean
Boolean b = toBoolean(o);
if (b != null) {
    return b.booleanValue();
return false;
byte[]toPrimitiveByteArray(Integer[] array)
to Primitive Byte Array
byte[] destination = new byte[array.length];
for (int i = 0; i < array.length; i++)
    destination[i] = array[i].byteValue();
return destination;
ClasstoPrimitiveClass(Class wrapperClass)
to Primitive Class
if (wrapperClass == Integer.class) {
    return Integer.TYPE;
} else if (wrapperClass == Byte.class) {
    return Byte.TYPE;
} else if (wrapperClass == Short.class) {
    return Short.TYPE;
} else if (wrapperClass == Long.class) {
    return Long.TYPE;
...
doubletoPrimitiveDouble(Double d)
<#if locale="en">

Convert to Double to double.If the Double value is null, return 0d.

if (d == null) {
    return 0d;
return d.doubleValue();
inttoPrimitiveInt(Integer value)
Converts an object Integer to a primitive int.
if (value == null) {
    return 0;
return value;
inttoPrimitiveInt(Object o)
Converts the object into primitive int.
return toPrimitiveBoolean(o) ? 1 : 0;
NumbertoPrimitiveNumber(String s)
Converts a string into its "closest" primitive type.
if (s == null) {
    return null;
s = s.trim();
try {
    int i = Integer.parseInt(s);
    return i;
} catch (NumberFormatException nfe) {
...