Java Short Number Create toShort(Object obj)

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

Description

to Short

License

Open Source License

Declaration

public static short toShort(Object obj) 

Method Source Code

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

public class Main {
    public static short toShort(Object obj) {
        return toShort(obj, (byte) 0);
    }//from w  w  w . java  2  s  . c  om

    public static short toShort(Object obj, short defaultValue) {
        if (obj == null) {
            return defaultValue;
        }

        if (obj instanceof Number) {
            Number number = (Number) obj;
            return number.shortValue();
        }
        String value = toString(obj);
        try {
            return Short.parseShort(value);
        } catch (Exception e) {
            return defaultValue;
        }
    }

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

Related

  1. toShort(Number num)
  2. toShort(Number value)
  3. toShort(Object o)
  4. toShort(Object ob, Short defaultShort)
  5. toShort(Object obj)
  6. toShort(Object obj)
  7. toShort(Object obj)
  8. toShort(Object prmIntObject)
  9. toShort(Object value)