mdoTomofumi Chiba
7/30/2024, 1:43:30 PM

Java: Object to long

long toLong(Object object) {
  if (object instanceof Long) {
    return (Long)object;
  } else if (object instanceof Integer) {
    return (Integer)object;
  } else if (object instanceof String) {
    return Long.parseLong((String)object);
  }
  return Long.parseLong(object.toString());
}

System.out.println(toLong(Integer.MAX_VALUE));
System.out.println(toLong(Long.MAX_VALUE));
System.out.println(toLong("1"));
TweetLike