Skip to content

Instantly share code, notes, and snippets.

@taichi
Created June 10, 2015 02:17
Show Gist options
  • Save taichi/855274c24a99db72545b to your computer and use it in GitHub Desktop.
Save taichi/855274c24a99db72545b to your computer and use it in GitHub Desktop.
public class ReturnTypeOverload {
public static void main(String[] args) {
Long lv = getValue();
System.out.println(lv);
String sv = getValue();
System.out.println(sv);
}
// public static Long get() {
// return 30L;
// }
//
// public static String get() {
// return "33";
// }
@SuppressWarnings("unchecked")
public static <T> T getValue(T... dontset) {
Class<?> type = dontset.getClass().getComponentType();
if (Long.class.equals(type)) {
return (T) Long.valueOf("30");
}
if (String.class.equals(type)) {
return (T) "33";
}
throw new IllegalStateException();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment