Java Integer Create toInteger(Object value)

Here you can find the source of toInteger(Object value)

Description

Convert an Object to an Integer.

License

Apache License

Declaration

public static Integer toInteger(Object value) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/* w  w  w. j a v  a  2 s.  c  o  m*/
     * Convert an Object to an Integer.
     */
    public static Integer toInteger(Object value) {
        if (value == null)
            return null;
        if (value instanceof Integer)
            return (Integer) value;
        if (value instanceof String) {
            if ("".equals((String) value))
                return null;
            return new Integer((String) value);
        }

        if (value instanceof Number)
            return new Integer(((Number) value).intValue());

        return new Integer(value.toString());
    }

    /**
     * Convert an Object to an int, or 0 if it is null.
     */
    public static int intValue(Object value) {
        if (value == null)
            return 0;
        return toInteger(value).intValue();
    }
}

Related

  1. toInteger(Object val)
  2. toInteger(Object val)
  3. toInteger(Object value)
  4. toInteger(Object value)
  5. toInteger(Object value)
  6. toInteger(Object value)
  7. toInteger(Object value)
  8. toInteger(Object value)
  9. toInteger(Object value, int defaultValue)