Created
October 1, 2014 19:10
-
-
Save katowulf/19d9516047feca474d7f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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