Prove that the interface for a primitive type is an empty array in Java

Java 8Object Oriented ProgrammingProgramming

The getInterfaces() method is used to prove that the interface for a primitive type is an empty array. A program that demonstrates this is given as follows −

Example

 Live Demo

package Test;
import java.util.*;
public class Demo {
   public static void main(String[] args) {
      Class c = int.class;
      Class[] interfaces = c.getInterfaces();
      System.out.println("The Interface is: " + Arrays.asList(interfaces));
   }
}

Output

The Interface is: []


Now let us understand the above program.

The int.class is a reference to the Class object for the primitive type int. The interface for this is determined using the getInterfaces() method. This is an empty array as is obvious when it is displayed. A code snippet which demonstrates this is as follows −

Class c = int.class;
Class[] interfaces = c.getInterfaces();
System.out.println("The Interface is: " + Arrays.asList(interfaces));


raja
Published on 09-Jan-2019 15:17:08
Advertisements