Check whether String is an interface or class in Java

Java 8Object Oriented ProgrammingProgramming

The method java.lang.Class.isInterface() is used to find if String is an interface or class in Java. This method returns true if String is an interface, otherwise it returns false.

A program that demonstrates this is given as follows −

Example

 Live Demo

package Test;
import java.lang.*;
public class Demo {
   public static void main(String[] args) {
      Class c = String.class;
      boolean interfaceFlag = c.isInterface();
      System.out.println("This is an Interface? " + interfaceFlag);
   }
}

Output

This is an Interface? false

Now let us understand the above program.

The isInterface() method is used to find if String is an interface or not. The result is stored in interfaceFlag and then displayed. A code snippet which demonstrates this is as follows −

Class c = String.class;
boolean interfaceFlag = c.isInterface();
System.out.println("This is an Interface? " + interfaceFlag);  
raja
Published on 09-Jan-2019 10:40:36
Advertisements