Java Object to int castInt(Object o)

Here you can find the source of castInt(Object o)

Description

cast Int

License

Open Source License

Declaration

public static Integer castInt(Object o) 

Method Source Code

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

public class Main {
    public static Integer castInt(Object o) {
        if (o == null) {
            return 0;
        } else if (o instanceof Byte) {
            return (int) (Byte) o;
        } else if (o instanceof Integer) {
            return (Integer) o;
        } else if (o instanceof Double) {
            return (int) (double) (Double) o;
        } else if (o instanceof Float) {
            return (int) (float) (Float) o;
        } else if (o instanceof Long) {
            return (int) (long) (Long) o;
        } else if (o instanceof String) {
            try {
                return Integer.parseInt((String) o);
            } catch (Exception ex) {
                return 0;
            }/*from w w w  .j  av  a2  s. c o m*/
        } else {
            return 0;
        }
    }
}

Related

  1. castInt(Object o)
  2. castToInt(double[] aDouble)
  3. castToInt(final long value)
  4. castToInt(Object value)