Java에서 데이터 유형을 확인하는 방법은 무엇입니까?

Java에서 데이터 유형을 확인하는 방법은 무엇입니까?

때로는 동일한 유형의 변수로 논리연산을 수행할 수 있기 때문에 데이터를 계산하기 위해 변수의 데이터 유형을 확인해야 할 때가 있습니다. 데이터 유형을 확인하기 위해 getClass() 및 getSimpleName() 메소드를 사용하여 각각 클래스와 이름을 가져옵니다. 이 섹션에서는 다음 내용을 논의하겠습니다. Java에서 데이터 유형을 확인하는 방법은 무엇입니까?

변수의 데이터 유형을 가져오는 코드를 구현해 보겠습니다. 먼저 사용자로부터 입력을 받은 다음 사용자 입력이 저장될 변수의 데이터 유형을 찾습니다.

CheckDataTypeExample.java

 import java.util.*; // create class CheckDataTypeExample to check the datatype of the variable public class CheckDataTypeExample { // main() method start public static void main(String args[]) { // declare variables int intData; char charData; // create Scanner class object to take input from user Scanner sc = new Scanner(System.in); // take input from the user to initialize variables System.out.println('Enter a String value:'); String str = sc.nextLine(); System.out.println('Enter Integer value:'); intData = sc.nextInt(); System.out.println('Enter Character value:'); charData = sc.next().charAt(0); // close Scanner class object sc.close(); // show 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()); } }  

산출:

Java에서 데이터 유형을 확인하는 방법

이제 특별한 방법이 있습니다. getType() java.lang.reflect.Field 및 Character 클래스에서 제공됩니다. 두 클래스의 getType() 메소드를 하나씩 이해해 봅시다.

필드.getType()

그만큼 getType() 의 방법 필드 클래스는 Field 개체에 의해 정의된 필드 유형을 가져오는 데 사용됩니다. 반환 값은 필드 유형을 식별하는 데 도움이 됩니다.

통사론:

구문은 getType() 방법은 다음과 같습니다.

 public String getType()  

매개변수: 매개변수로 인수를 허용하지 않습니다.

보고: 필드 유형을 식별하는 데 도움이 되는 클래스 객체를 반환합니다.

getType() 메소드의 예를 들어 그것이 어떻게 작동하는지 이해해 봅시다:

GetTypeExample1.java

 // import required classes and package if any import java.lang.reflect.Field; // create class GetTypeExample1 to get the type of the Field public class GetTypeExample1 { // main() method start public static void main(String[] args)throws Exception { //get the name field object by using getField() method Field nameField = Student.class.getField('name'); // use getTyoe() method of the Field to get the type of name field Class value = nameField.getType(); // print the type of name field System.out.println('The type of the name field is ' + value); //get the totalMarks field object by using getField() method Field marksField = Student.class.getField('totalMarks'); // use getTyoe() method of the Field to get the type of totalMarks field value = marksField.getType(); // print the type of name field System.out.println('The type of the totalMarks field is ' + value); //get the totalFees field object by using getField() method Field feesField = Student.class.getField('totalFees'); // use getTyoe() method of the Field to get the type of name field value = feesField.getType(); // print the type of the totalFees field System.out.println('The type of the totalFees field is ' + value); } } // create a simple student class class Student { // declare and initialize variables public static String name = 'John'; public static double totalMarks = 380; public static float totalFees = 3413.99f; // getter for student name public static String getName() { return name; } // setter for student name public static void setName(String stdName) { name = stdName; } // getter for totalMarks public static double getTotalMarks() { return totalMarks; } // setter for totalMarks public static void setMarks(double marks) { totalMarks = marks; } // getter for totalFees public static float getTotalFees() { return totalFees; } // setter for totalFees public static void setFees(float fees) { totalFees = fees; } }  

산출:

Java에서 데이터 유형을 확인하는 방법

Field.getType() 메서드 사용

그만큼 getType() 의 방법 성격 클래스는 주어진 캐릭터의 일반 카테고리를 얻는 데 사용됩니다. getType() 메소드는 매개변수에 따라 두 가지 변형이 제공됩니다. Character.getType(문자 ch) 그리고 Character.getType(int codePoint) .

char을 매개변수로 하는 getType() 메소드는 보조문자를 처리할 수 없지만, int를 매개변수로 하는 getType() 메소드는 보조문자를 처리할 수 있다.

통사론:

그만큼 getType() 의 방법 성격 클래스에는 다음과 같은 구문이 있습니다.

 public static int getType(char ch) public static int getType(int codePoint)  

매개변수: getType() 메소드의 첫 번째 변형은 유형의 매개변수를 허용합니다. 메소드의 두 번째 변형은 int 유형의 매개변수, 즉 codePoint를 허용합니다.

보고: 두 메서드 모두 문자의 일반 범주를 나타내는 정수 값을 반환합니다.

getType() 메소드의 예를 들어 그것이 어떻게 작동하는지 이해해 봅시다:

GetTypeExample2.java

 // import required classes and package if any // create class GetTypeExample2 to get the general category of the given character public class GetTypeExample2 { // main() method start public static void main(String[] args)throws Exception { // use setter to set ch1, ch2 in CharData CharData.setChar1('C'); CharData.setChar2('%'); // use getter to get char1 and char2 char char1 = CharData.getChar1(); char char2 = CharData.getChar2(); // use getType() method of Character class int val1 = Character.getType(char1); int val2 = Character.getType(char2); // print categories of char1 and char2 System.out.println('The category of ' +char1 + ' is '+ val1); System.out.println('The category of ' +char2 + ' is '+ val2); } } // create a simple CharData class class CharData { // declare variables of type char static char ch1, ch2; // getter for ch1 public static char getChar1() { return ch1; } // setter for ch1 public static void setChar1(char ch) { ch1 = ch; } // getter for ch2 public static char getChar2() { return ch2; } // setter for ch2 public static void setChar2(char ch) { ch2 = ch; } }  

산출:

Java에서 데이터 유형을 확인하는 방법

GetTypeExample3.java

 // import required classes and package if any import java.lang.reflect.Field; // create class GetTypeExample3 to get the general category of the given character public class GetTypeExample3 { // main() method start public static void main(String[] args)throws Exception { // use setter to set code1, code2 in CodePoint CodePoint.setCodePoint1(0x0037); CodePoint.setCodePoint2(0x016f); // use getter to get code1 and code2 int code1 = CodePoint.getCodePoint1(); int code2 = CodePoint.getCodePoint2(); // use getType() method of Character class int val1 = Character.getType(code1); int val2 = Character.getType(code2); // print categories of char1 and char2 System.out.println('The category of ' +code1+ ' is '+ val1); System.out.println('The category of ' +code2+ ' is '+ val2); } } // create a simple CodePoint class class CodePoint { // declare variables of type int static int codePoint1, codePoint2; // getter for codePoint1 public static int getCodePoint1() { return codePoint1; } // setter for codePoint1 public static void setCodePoint1(int code1) { codePoint1 = code1; } // getter for codePoint2 public static int getCodePoint2() { return codePoint2; } // setter for codePoint2 public static void setCodePoint2(int code2) { codePoint2 = code2; } }  

산출:

Java에서 데이터 유형을 확인하는 방법