How to check if an object is instance is of a specified type in Java

1 Answer

0 votes
public class MyClass {
    public static void main(String args[]) {
        String str = "java";
  
        if (str instanceof String) {
            System.out.println("str is instance of String"); 
        }
        else {
            System.out.println("str is NOT instance of String"); 
        }  
    }
}




/*
run:

str is instance of String

*/

 



answered Jun 6, 2023 by avibootz
...