How to check data type in Java
In this section, we will learn about the methodology to verify the data type in Java. There are mainly two methods in java called as getClass() and getName(). They are leveraged in order to check the data type in java.
Since, we can execute the logic operations using the identical type of variables, we occasionally need to verify the data type of such a variable before computing data. We utilize the getClass() and getSimpleName() methods to obtain the class and name of the data, respectively, in order to check the data type. We will learn about how to check the data type in Java in this article.
File name: Verifydtatatype1.java
import java.util.*;
// To validate the variable's datatype, create the class Verifydatatype.
public class CheckDataTypeExample {
// this is the main method()
public static void main(String args[]) {
// declaration of variables
int intData;
char charData;
//Creation of scanner class for considering the variables from the user
Scanner s = new Scanner(System.in);
//Considering inputs from the user to initialize variables
System.out.println("Pass the string value:");
String str = s.nextLine();
System.out.println("Pass the integer value:");
intData = s.nextInt();
System.out.println("Pass the character value:");
charData = s.next().charAt(0);
// Closing the scanner class object
s.close();
// Displays datatypes of variables by using getClass() and getSimpleName() methods
System.out.println(intData + " is of type " + ((Object)intData).getClass().getSimpleName());
System.out.println(charData + " is of type " + ((Object)charData).getClass().getSimpleName());
System.out.println(str + " is of type " + str.getClass().getSimpleName());
}
}
Output
Pass the string value:
Arya
Pass the integer value:
143
Pass the character value:
r
143 is of type Integer
r is of type Character
Arya is of type String
We now have a unique method, getType(), which is made available by the java.lang.reflect.Field as well as Character classes. Let's examine each class's getType() method individually.
To determine the type of field that the Field object defines, use the getType() function of the Field class. We can identify the type of variable by looking at the return value.
To get the broad category of a given character, utilize the getType() function of the Character class. Two variations of the getType() method are available depending on the parameter, which is a character. Character and getType(char ch). getType(int codePoint) (int codePoint).
Supplementary characters cannot be handled by the getType() function that accepts a char as a parameter; however, they can be handled by the getType() method which accepts an int as a parameter.
File name: Verifydatatype2.java
import java.lang.reflect.Field;
//To get the type of data type of variable, the class is created
public class Verifydatatype2 {
public static void main(String[] args)throws Exception
{
Field nameField = Engineer.class.getField("name");
// using getType() method of the variable to the type of the data
Class value = nameField.getType();
//displaying the type of data type
System.out.println("The type of the name field is " + value);
//get the totalDays field object by using getField() method
Field daysField = Engineer.class.getField("totalDays");
// use getTyoe() method of the Field to get the type of totalDays field
value = daysField.getType();
// print the type of name field
System.out.println("The type of the totalDays field is " + value);
//get the totalSalary field object by using getField() method
Field salaryField = Engineer.class.getField("totalSalary");
// use getTyoe() method of the Field to get the type of name field
value = salaryField.getType();
// print the type of the totalSalary field
System.out.println("The type of the totalSalary field is " + value);
}
}
// create a simple Engineer class
class Engineer {
// declrartion and initialization of the variables
public static String name = "John";
public static double totalDays = 300;
public static float totalSalary = 3413.99f;
// getter for Engineer name
public static String getName()
{
return name;
}
// setter for Engineer name
public static void setName(String stdName)
{
name = stdName;
}
// getter for totalDays
public static double getTotalDays()
{
return totalDays;
}
// setter for totalDays
public static void setDays(double days)
{
totalDays = days;
}
// getter for totalSalary
public static float getTotalSalary()
{
return totalSalary;
}
// setter for totalSalary
public static void setSalary(float salary)
{
totalSalary = salary;
}
}
Output
The type of the name field is class java.lang.String
The type of the totalDays field is double
The type of the totalSalary field is float