「Java入門」Object#getClass()を使うサンプル
サンプル
package test;
import java.io.Serializable;
public class GetClassDemo {
public static void main(String[] args) {
final Number dbj = new Integer(256);
if (dbj.getClass() == (Class<?>) Integer.class) {
Integer ig = (Integer) dbj;
System.out.println(“Integer Value : " + ig);
}
if (dbj.getClass() == (Class<?>) Double.class) {
Double db = (Double) dbj;
System.out.println(“Double Value: " + db);
}
if (dbj.getClass() == (Class<?>) Serializable.class) {
Serializable sb = dbj;
System.out.println(“Serializable Value: " + sb);
}
if (dbj.getClass() == (Class<?>) Object.class) {
Object oo = dbj;
System.out.println(“Object Value: " + oo);
}
if (dbj.getClass() == (Class<?>) Number.class) {
Number nn = dbj;
System.out.println(“Number Value: " + nn);
}
if (dbj.getClass() == (Class<?>) Runnable.class) {
Runnable rr = (Runnable) dbj;
System.out.println(“Runnable Value: " + rr);
}
}
}
結果
Integer Value : 256