Here you can find the source of toLong(Object o)
public static Long toLong(Object o)
//package com.java2s; //License from project: LGPL public class Main { public static Long toLong(Object o) { if (o == null) return null; if (o.getClass() == Long.class) return (Long) o; if (o.getClass() == Boolean.class) return booleanToLong((Boolean) o); if (o.getClass() == Byte.class) return (long) ((Byte) o).byteValue(); if (o.getClass() == Character.class) return (long) ((Character) o).charValue(); if (o.getClass() == Short.class) return (long) ((Short) o).shortValue(); if (o.getClass() == Integer.class) return (long) ((Integer) o).intValue(); if (o.getClass() == Float.class) return floatToLong((Float) o); if (o.getClass() == Double.class) return doubleToLong((Double) o); if (o.getClass() == String.class) return stringToLong((String) o); return null; }//from w w w . j a v a 2s.c om public static long toLong(Object o, long defaultValue) { if (o == null) return defaultValue; if (o.getClass() == Long.class) return (Long) o; if (o.getClass() == Boolean.class) return booleanToLong((Boolean) o); if (o.getClass() == Byte.class) return (long) ((Byte) o).byteValue(); if (o.getClass() == Character.class) return (long) ((Character) o).charValue(); if (o.getClass() == Short.class) return (long) ((Short) o).shortValue(); if (o.getClass() == Integer.class) return (long) ((Integer) o).intValue(); if (o.getClass() == Float.class) return floatToLong((Float) o, defaultValue); if (o.getClass() == Double.class) return doubleToLong((Double) o, defaultValue); if (o.getClass() == String.class) return stringToLong((String) o, defaultValue); return defaultValue; } public static long booleanToLong(boolean b) { return b ? 1L : 0L; } public static Long floatToLong(float f) { if (f >= Long.MIN_VALUE && f <= Long.MAX_VALUE) { return (long) f; } return null; } public static long floatToLong(float f, long defaultValue) { if (f >= Long.MIN_VALUE && f <= Long.MAX_VALUE) { return (long) f; } return defaultValue; } public static Long doubleToLong(double d) { if (d >= Long.MIN_VALUE && d <= Long.MAX_VALUE) { return (long) d; } return null; } public static long doubleToLong(double d, long defaultValue) { if (d >= Long.MIN_VALUE && d <= Long.MAX_VALUE) { return (long) d; } return defaultValue; } public static Long stringToLong(String s) { try { return Long.valueOf(s); } catch (NumberFormatException e) { return null; } } public static long stringToLong(String s, long defaultValue) { try { return Long.valueOf(s); } catch (NumberFormatException e) { return defaultValue; } } }