Java Byte Create toByte(Object obj)

Here you can find the source of toByte(Object obj)

Description

to Byte

License

Open Source License

Declaration

public static byte toByte(Object obj) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static byte toByte(Object obj) {
        return toByte(obj, (byte) 0);
    }/*from w  w  w.java2 s.  c o m*/

    public static byte toByte(Object obj, byte defaultValue) {
        if (obj == null) {
            return defaultValue;
        }

        if (obj instanceof Number) {
            Number number = (Number) obj;
            return number.byteValue();
        }
        String value = toString(obj);
        try {
            return Byte.parseByte(value);
        } catch (Exception e) {
        }
        return defaultValue;
    }

    public static String toString(Object value) {
        if (value == null) {
            return "";
        }
        return value.toString().trim();
    }
}

Related

  1. toByte(long l)
  2. toByte(long l, byte[] b, int off, boolean bigEndian)
  3. toByte(Number num)
  4. toByte(Object o)
  5. toByte(Object ob, Byte defaultByte)
  6. toByte(Object obj)
  7. toByte(Object obj)
  8. toByte(Object obj)
  9. toByte(Object value)