package dfa; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.List; public class InstanceofFromPrimitiveToPrimitive { public static void main(String[] args) { System.out.println("testBoolean"); testBoolean(); System.out.println("testInt"); testInt(); System.out.println("testChar"); testChar(); System.out.println("testByte"); testByte(); System.out.println("testShort"); testShort(); System.out.println("testLong"); testLong(); System.out.println("testFloat"); testFloat(); System.out.println("testDouble"); testDouble(); System.out.println("testWithInference"); testWithInference(new ArrayList<>()); } private static void testWithInference(List<@NotNull Short> list) { if(list.get(0) instanceof int) { //redundant } } private static void testDouble() { System.out.println(0); double o = 0; if (o instanceof int t) { //true System.out.println("int" + t); } if (o instanceof char t) { //true System.out.println("char" + t); } if (o instanceof byte t) { //true System.out.println("byte" + t); } if (o instanceof short t) { //true System.out.println("short" + t); } if (o instanceof long t) { //true System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("Float.MIN_VALUE"); o = Float.MIN_VALUE; if (o instanceof int t) { //false System.out.println("int" + t); } if (o instanceof char t) { //false System.out.println("char" + t); } if (o instanceof byte t) { //false System.out.println("byte" + t); } if (o instanceof short t) { //false System.out.println("short" + t); } if (o instanceof long t) { //false System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("Float.MAX_VALUE"); o = Float.MAX_VALUE; if (o instanceof int t) { //false System.out.println("int" + t); } if (o instanceof char t) { //false System.out.println("char" + t); } if (o instanceof byte t) { //false System.out.println("byte" + t); } if (o instanceof short t) { //false System.out.println("short" + t); } if (o instanceof long t) { //false System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("Double.MIN_VALUE"); o = Double.MIN_VALUE; if (o instanceof int t) { //false System.out.println("int" + t); } if (o instanceof char t) { //false System.out.println("char" + t); } if (o instanceof byte t) { //false System.out.println("byte" + t); } if (o instanceof short t) { //false System.out.println("short" + t); } if (o instanceof long t) { //false System.out.println("long" + t); } if (o instanceof float t) { //false System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("Double.MAX_VALUE"); o = Double.MAX_VALUE; if (o instanceof int t) { //false System.out.println("int" + t); } if (o instanceof char t) { //false System.out.println("char" + t); } if (o instanceof byte t) { //false System.out.println("byte" + t); } if (o instanceof short t) { //false System.out.println("short" + t); } if (o instanceof long t) { //false System.out.println("long" + t); } if (o instanceof float t) { //false System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("0x1p63"); o = 0x1p63; if (o instanceof int t) { //false System.out.println("int" + t); } if (o instanceof char t) { //false System.out.println("char" + t); } if (o instanceof byte t) { //false System.out.println("byte" + t); } if (o instanceof short t) { //false System.out.println("short" + t); } if (o instanceof long t) { //false System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("0x1p5"); o = 0x1p5; if (o instanceof int t) { //true System.out.println("int" + t); } if (o instanceof char t) { //true System.out.println("char" + t); } if (o instanceof byte t) { //true System.out.println("byte" + t); } if (o instanceof short t) { //true System.out.println("short" + t); } if (o instanceof long t) { //true System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("0x1p62"); o = 0x1p62; if (o instanceof int t) { //false System.out.println("int" + t); } if (o instanceof char t) { //false System.out.println("char" + t); } if (o instanceof byte t) { //false System.out.println("byte" + t); } if (o instanceof short t) { //false System.out.println("short" + t); } if (o instanceof long t) { //true System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double) { //true System.out.println("double"); } System.out.println("nan"); o = Double.NaN; if (o instanceof int t) { //false System.out.println("int" + t); } if (o instanceof char t) { //false System.out.println("char" + t); } if (o instanceof byte t) { //false System.out.println("byte" + t); } if (o instanceof short t) { //false System.out.println("short" + t); } if (o instanceof long t) { //false System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("float positive inf"); o = Float.POSITIVE_INFINITY; if (o instanceof int t) { //false System.out.println("int" + t); } if (o instanceof char t) { //false System.out.println("char" + t); } if (o instanceof byte t) { //false System.out.println("byte" + t); } if (o instanceof short t) { //false System.out.println("short" + t); } if (o instanceof long t) { //false System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("float negative inf"); o = Float.NEGATIVE_INFINITY; if (o instanceof int t) { //false System.out.println("int" + t); } if (o instanceof char t) { //false System.out.println("char" + t); } if (o instanceof byte t) { //false System.out.println("byte" + t); } if (o instanceof short t) { //false System.out.println("short" + t); } if (o instanceof long t) { //false System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("double positive inf"); o = Double.POSITIVE_INFINITY; if (o instanceof int t) { //false System.out.println("int" + t); } if (o instanceof char t) { //false System.out.println("char" + t); } if (o instanceof byte t) { //false System.out.println("byte" + t); } if (o instanceof short t) { //false System.out.println("short" + t); } if (o instanceof long t) { //false System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("double negative inf"); o = Double.NEGATIVE_INFINITY; if (o instanceof int t) { //false System.out.println("int" + t); } if (o instanceof char t) { //false System.out.println("char" + t); } if (o instanceof byte t) { //false System.out.println("byte" + t); } if (o instanceof short t) { //false System.out.println("short" + t); } if (o instanceof long t) { //false System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("-0.0"); o = -0.0f; if (o instanceof int t) {//false System.out.println("int" + t); } if (o instanceof char t) {//false System.out.println("char" + t); } if (o instanceof byte t) {//false System.out.println("byte" + t); } if (o instanceof short t) {//false System.out.println("short" + t); } if (o instanceof long t) { //false System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } } private static void testFloat() { System.out.println(0); float o = 0; if (o instanceof int t) { //true System.out.println("int" + t); } if (o instanceof char t) { //true System.out.println("char" + t); } if (o instanceof byte t) { //true System.out.println("byte" + t); } if (o instanceof short t) { //true System.out.println("short" + t); } if (o instanceof long t) { //true System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("Float.MIN_VALUE"); o = Float.MIN_VALUE; if (o instanceof int t) { //false System.out.println("int" + t); } if (o instanceof char t) { //false System.out.println("char" + t); } if (o instanceof byte t) { //false System.out.println("byte" + t); } if (o instanceof short t) { //false System.out.println("short" + t); } if (o instanceof long t) { //false System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("Float.MAX_VALUE"); o = Float.MAX_VALUE; if (o instanceof int t) { //false System.out.println("int" + t); } if (o instanceof char t) { //false System.out.println("char" + t); } if (o instanceof byte t) { //false System.out.println("byte" + t); } if (o instanceof short t) { //false System.out.println("short" + t); } if (o instanceof long t) { //false System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("0x1p63f"); o = 0x1p63f; if (o instanceof int t) { //false System.out.println("int" + t); } if (o instanceof char t) { //false System.out.println("char" + t); } if (o instanceof byte t) { //false System.out.println("byte" + t); } if (o instanceof short t) { //false System.out.println("short" + t); } if (o instanceof long t) { //false System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("0x1p5f"); o = 0x1p5f; if (o instanceof int t) { //true System.out.println("int" + t); } if (o instanceof char t) { //true System.out.println("char" + t); } if (o instanceof byte t) { //true System.out.println("byte" + t); } if (o instanceof short t) { //true System.out.println("short" + t); } if (o instanceof long t) { //true System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("0x1p62f"); o = 0x1p62f; if (o instanceof int t) { //false System.out.println("int" + t); } if (o instanceof char t) { //false System.out.println("char" + t); } if (o instanceof byte t) { //false System.out.println("byte" + t); } if (o instanceof short t) { //false System.out.println("short" + t); } if (o instanceof long t) { //true System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("nan"); o = Float.NaN; if (o instanceof int t) { //false System.out.println("int" + t); } if (o instanceof char t) { //false System.out.println("char" + t); } if (o instanceof byte t) { //false System.out.println("byte" + t); } if (o instanceof short t) { //false System.out.println("short" + t); } if (o instanceof long t) { //false System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("positive inf"); o = Float.POSITIVE_INFINITY; if (o instanceof int t) { //false System.out.println("int" + t); } if (o instanceof char t) { //false System.out.println("char" + t); } if (o instanceof byte t) { //false System.out.println("byte" + t); } if (o instanceof short t) { //false System.out.println("short" + t); } if (o instanceof long t) { //false System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("negative inf"); o = Float.NEGATIVE_INFINITY; if (o instanceof int t) { //false System.out.println("int" + t); } if (o instanceof char t) { //false System.out.println("char" + t); } if (o instanceof byte t) { //false System.out.println("byte" + t); } if (o instanceof short t) { //false System.out.println("short" + t); } if (o instanceof long t) { //false System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("-0.0"); o = -0.0f; if (o instanceof int t) {//false System.out.println("int" + t); } if (o instanceof char t) {//false System.out.println("char" + t); } if (o instanceof byte t) {//false System.out.println("byte" + t); } if (o instanceof short t) {//false System.out.println("short" + t); } if (o instanceof long t) { //false System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("0x1p31f"); o = 0x1p31f; if (o instanceof int t) {//false System.out.println("int" + t); } if (o instanceof char t) {//false System.out.println("char" + t); } if (o instanceof byte t) {//false System.out.println("byte" + t); } if (o instanceof short t) {//false System.out.println("short" + t); } if (o instanceof long t) { //true System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } } private static void testLong() { System.out.println(0); long o = 0; if (o instanceof int t) { //true System.out.println("int" + t); } if (o instanceof char t) { //true System.out.println("char" + t); } if (o instanceof byte t) { //true System.out.println("byte" + t); } if (o instanceof short t) { //true System.out.println("short" + t); } if (o instanceof long t) { //true System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("Long.MIN_VALUE"); o = Long.MIN_VALUE; if (o instanceof int t) { //false System.out.println("int" + t); } if (o instanceof char t) { //false System.out.println("char" + t); } if (o instanceof byte t) { //false System.out.println("byte" + t); } if (o instanceof short t) { //false System.out.println("short" + t); } if (o instanceof long t) { //true System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("Long.MAX_VALUE"); o = Long.MAX_VALUE; if (o instanceof int) { //false System.out.println("int"); } if (o instanceof char t) { //false System.out.println("char" + t); } if (o instanceof byte t) { //false System.out.println("byte" + t); } if (o instanceof short t) { //false System.out.println("short" + t); } if (o instanceof long t) { //true System.out.println("long" + t); } if (o instanceof float t) { //false System.out.println("float" + t); } if (o instanceof double t) { //false System.out.println("double" + t); } System.out.println("Long.MAX_VALUE - (long)1e19"); o = Long.MAX_VALUE - (long) 9.22e18; if (o instanceof int) { //false System.out.println("int"); } if (o instanceof char t) { //false System.out.println("char" + t); } if (o instanceof byte t) { //false System.out.println("byte" + t); } if (o instanceof short t) { //false System.out.println("short" + t); } if (o instanceof long t) { //true System.out.println("long" + t); } if (o instanceof float t) { //false System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("0x001fffffffffffffL"); o = 0x001fffffffffffffL; if (o instanceof int) { //false System.out.println("int"); } if (o instanceof char t) { //false System.out.println("char" + t); } if (o instanceof byte t) { //false System.out.println("byte" + t); } if (o instanceof short t) { //false System.out.println("short" + t); } if (o instanceof long t) { //true System.out.println("long" + t); } if (o instanceof float t) { //false System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } } private static void testShort() { System.out.println(0); short o = 0; if (o instanceof int t) { //true System.out.println("int" + t); } if (o instanceof char t) { //true System.out.println("char" + t); } if (o instanceof byte t) { //true System.out.println("byte" + t); } if (o instanceof short t) { //true System.out.println("short" + t); } if (o instanceof long t) { //true System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("Short.MIN_VALUE"); o = Short.MIN_VALUE; if (o instanceof int t) { //true System.out.println("int" + t); } if (o instanceof char t) { //false System.out.println("char" + t); } if (o instanceof byte t) { //false System.out.println("byte" + t); } if (o instanceof short t) { //true System.out.println("short" + t); } if (o instanceof long t) { //true System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("Short.MAX_VALUE"); o = Short.MAX_VALUE; if (o instanceof int) { //true System.out.println("int"); } if (o instanceof char t) { //true System.out.println("char" + t); } if (o instanceof byte t) { //false System.out.println("byte" + t); } if (o instanceof short t) { //true System.out.println("short" + t); } if (o instanceof long t) { //true System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("Byte.MIN_VALUE"); o = Byte.MIN_VALUE; if (o instanceof int t) { //true System.out.println("int" + t); } if (o instanceof char t) { //false System.out.println("char" + t); } if (o instanceof byte t) { //true System.out.println("byte" + t); } if (o instanceof short t) { //true System.out.println("short" + t); } if (o instanceof long t) { //true System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("Byte.MAX_VALUE"); o = Byte.MAX_VALUE; if (o instanceof int) { //true System.out.println("int"); } if (o instanceof char t) { //true System.out.println("char" + t); } if (o instanceof byte t) { //true System.out.println("byte" + t); } if (o instanceof short t) { //true System.out.println("short" + t); } if (o instanceof long t) { //true System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } } private static void testByte() { System.out.println(0); byte o = 0; if (o instanceof int t) { //true System.out.println("int" + t); } if (o instanceof char t) { //true System.out.println("char" + t); } if (o instanceof byte t) { //true System.out.println("byte" + t); } if (o instanceof short t) { //true System.out.println("short" + t); } if (o instanceof long t) { //true System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("Byte.MIN_VALUE"); o = Byte.MIN_VALUE; if (o instanceof int t) { //true System.out.println("int" + t); } if (o instanceof char t) { //false System.out.println("char" + t); } if (o instanceof byte t) { //true System.out.println("byte" + t); } if (o instanceof short t) { //true System.out.println("short" + t); } if (o instanceof long t) { //true System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("Byte.MAX_VALUE"); o = Byte.MAX_VALUE; if (o instanceof int) { //true System.out.println("int"); } if (o instanceof char t) { //true System.out.println("char" + t); } if (o instanceof byte t) { //true System.out.println("byte" + t); } if (o instanceof short t) { //true System.out.println("short" + t); } if (o instanceof long t) { //true System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } } private static void testChar() { System.out.println(0); char o = 0; if (o instanceof int t) { //true System.out.println("int" + t); } if (o instanceof char t) { //true System.out.println("char" + t); } if (o instanceof byte t) { //true System.out.println("byte" + t); } if (o instanceof short t) { //true System.out.println("short" + t); } if (o instanceof long t) { //true System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("Character.MIN_VALUE"); o = Character.MIN_VALUE; if (o instanceof int t) { //true System.out.println("int" + t); } if (o instanceof char t) { //true System.out.println("char" + t); } if (o instanceof byte t) { //true System.out.println("byte" + t); } if (o instanceof short t) { //true System.out.println("short" + t); } if (o instanceof long t) { //true System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("Character.MAX_VALUE"); o = Character.MAX_VALUE; if (o instanceof int t) { //true System.out.println("int" + t); } if (o instanceof char t) { //true System.out.println("char" + t); } if (o instanceof byte t) { //false System.out.println("byte" + t); } if (o instanceof short t) { //false System.out.println("short" + t); } if (o instanceof long t) { //true System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } } private static void testInt() { System.out.println(0); int o = 0; if (o instanceof int t) { //true System.out.println("int" + t); } if (o instanceof char t) { //true System.out.println("char" + t); } if (o instanceof byte t) { //true System.out.println("byte" + t); } if (o instanceof short t) { //true System.out.println("short" + t); } if (o instanceof long t) { //true System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("shortMax"); o = Short.MAX_VALUE; if (o instanceof int t) { //true System.out.println("int" + t); } if (o instanceof char t) { //true System.out.println("char" + t); } if (o instanceof byte t) { //false System.out.println("byte" + t); } if (o instanceof short t) { //true System.out.println("short" + t); } if (o instanceof long t) { //true System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("intMax"); o = Integer.MAX_VALUE; if (o instanceof int t) { //true System.out.println("int" + t); } if (o instanceof char t) { //false System.out.println("char" + t); } if (o instanceof byte t) { //false System.out.println("byte" + t); } if (o instanceof short t) { //false System.out.println("short" + t); } if (o instanceof long t) { //true System.out.println("long" + t); } if (o instanceof float t) { //false System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } System.out.println("intMax"); o = Integer.MIN_VALUE; if (o instanceof int t) { //true System.out.println("int" + t); } if (o instanceof char t) { //false System.out.println("char" + t); } if (o instanceof byte t) { //false System.out.println("byte" + t); } if (o instanceof short t) { //false System.out.println("short" + t); } if (o instanceof long t) { //true System.out.println("long" + t); } if (o instanceof float t) { //true System.out.println("float" + t); } if (o instanceof double t) { //true System.out.println("double" + t); } } private static void testBoolean() { System.out.println("true"); boolean o = true; if (o instanceof boolean b) { System.out.println("boolean"); System.out.println(b); } System.out.println("false"); o = false; if (o instanceof boolean) { System.out.println("boolean"); } } }