Skip to content

Instantly share code, notes, and snippets.

@katowulf
Created October 1, 2014 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katowulf/19d9516047feca474d7f to your computer and use it in GitHub Desktop.
Save katowulf/19d9516047feca474d7f to your computer and use it in GitHub Desktop.
// inspiration: http://stackoverflow.com/questions/3993982/how-to-check-type-of-variable-in-java/16717058#16717058
class TypeTester {
void printType(Byte x) {
System.out.println(x + " is a byte");
}
void printType(Boolean x) {
System.out.println(x + " is a boolean");
}
void printType(Integer x) {
System.out.println(x + " is an int");
}
void printType(Float x) {
System.out.println(x + " is a float");
}
void printType(Double x) {
System.out.println(x + " is a double");
}
void printType(Character x) {
System.out.println(x + " is a char");
}
void printType(String x) {
System.out.println(x + " is a String");
}
void printType(Object x) {
if( x instanceof Byte ) {
this.printType((Byte)x);
}
else if( x instanceof Boolean ) {
this.printType((Boolean)x);
}
else if( x instanceof Integer ) {
this.printType((Integer)x);
}
else if( x instanceof Float ) {
this.printType((Float)x);
}
else if( x instanceof Double ) {
this.printType((Double)x);
}
else if( x instanceof Character ) {
this.printType((Character)x);
}
else if( x instanceof String ) {
this.printType((String)x);
}
else {
System.out.println(x + " is of unknown type");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment