public class IsInstanceOfTest {
public static void main(final String[] args) {
String s;
s = "";
System.out.println((s instanceof String));System.out.println(String.class.isInstance(s));
s = null;
System.out.println((s instanceof String));System.out.println(String.class.isInstance(s));}}
public class Test{public static void test(A a){System.out.println("a instanceof A: " + (a instanceof A));}
public static void test(B b) {// Overloaded version. Would cause reference ambiguity (compile error)// if Test.test(null) was called without casting.// So you need to call Test.test((A)null) or Test.test((B)null).}}
public static void main(String[] args) {Object testObject = "I am a string";List<Object> testList = null;if (testList instanceof List) {System.out.println("instance of list");} else {System.out.println("null type");}//Enhanced instanceof with type conversion - tested with JDK 17if (testObject instanceof String str) {System.out.println(str.toUpperCase());}}