「Java入門」instanceof演算子の基本使い方

サンプル
package test;

import java.io.Serializable;

public class InstanceOfDemo {

public static void main(String[] args) {
final Number cft = new Integer(345);

if (cft instanceof Object) {
Object x = cft;
System.out.println(“Object Value: ” + x);
}

if (cft instanceof Number) {
Number x = cft;
System.out.println(“Number Value: ” + x);
}

if (cft instanceof Double) {
Double x = (Double) cft;
System.out.println(“Double Value: ” + x);
}

if (cft instanceof Serializable) {
Serializable x = cft;
System.out.println(“Serializable Value:” + x);
}

if (cft instanceof Runnable) {
Runnable x = (Runnable) cft;
System.out.println(“Runnable Value: ” + x);
}

if (cft instanceof Integer) {
Integer x = (Integer) cft;
System.out.println(“Integer Value: ” + x);
}

}

}

結果
Object Value: 345
Number Value: 345
Serializable Value:345
Integer Value: 345

Comments are closed.