The getName() method is used to get the names of the entities such as primitive type, interface, class, array class, void etc. that represented by the class objects. These names are returned in the form of a string.
A program that gets the name of a primitive type using getName() method is given as follows -
public class Demo { public static void main(String[] argv) throws Exception { String name1 = float.class.getName(); System.out.println(name1); String name2 = int.class.getName(); System.out.println(name2); String name3 = char.class.getName(); System.out.println(name3); } }
float int char
Now let us understand the above program.
The getName() method is used to get the names of the primitive data types float, int and char. Then these names are stored in name1, name2 and name3 respectivel and displayed. A code snippet which demonstrates this is as follows −
String name1 = float.class.getName(); System.out.println(name1); String name2 = int.class.getName(); System.out.println(name2); String name3 = char.class.getName(); System.out.println(name3);