- Как проверить тип объекта java
- Java как проверить переменную
- Learn Latest Tutorials
- Preparation
- Trending Technologies
- B.Tech / MCA
- Javatpoint Services
- Training For College Campus
- Как узнать тип переменной java
- Check Type of a Variable in Java
- Use getClass().getSimpleName() to Check the Type of a Variable in Java
- Related Article — Java Data Type
Как проверить тип объекта java
В Java можно узнать тип переменной, используя оператор instanceof . Он позволяет проверить, является ли объект экземпляром определенного класса.
public class Main public static void main(String[] args) String str = "Hello, Hexlet!"; Integer integer = 123; System.out.println(str instanceof String); // => true System.out.println(integer instanceof Integer); // => true System.out.println(str instanceof Object); // => true System.out.println(integer instanceof Object); // => true > >
В этом примере мы объявляем переменные str и integer , типы которых String и Integer соответственно. Затем мы используем оператор instanceof для проверки, являются ли эти переменные экземплярами классов String , Integer или Object .
Как видно из примера, переменная str является экземпляром класса String , а переменная integer — экземпляром класса Integer . Кроме того, обе переменные также являются экземплярами класса Object , так как все классы в Java наследуются от этого класса.
Java как проверить переменную
Learn Latest Tutorials
Preparation
Trending Technologies
B.Tech / MCA
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
- Website Designing
- Website Development
- Java Development
- PHP Development
- WordPress
- Graphic Designing
- Logo
- Digital Marketing
- On Page and Off Page SEO
- PPC
- Content Development
- Corporate Training
- Classroom and Online Training
- Data Entry
Training For College Campus
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week
Like/Subscribe us for latest updates or newsletter
Как узнать тип переменной java
В Java можно узнать тип переменной, используя оператор instanceof . Он позволяет проверить, является ли объект экземпляром определенного класса.
public class Main public static void main(String[] args) String str = "Hello, Hexlet!"; Integer integer = 123; System.out.println(str instanceof String); // => true System.out.println(integer instanceof Integer); // => true System.out.println(str instanceof Object); // => true System.out.println(integer instanceof Object); // => true > >
В этом примере мы объявляем переменные str и integer , типы которых String и Integer соответственно. Затем мы используем оператор instanceof для проверки, являются ли эти переменные экземплярами классов String , Integer или Object .
Как видно из примера, переменная str является экземпляром класса String , а переменная integer — экземпляром класса Integer . Кроме того, обе переменные также являются экземплярами класса Object , так как все классы в Java наследуются от этого класса.
Check Type of a Variable in Java
This tutorial discusses the method to check the type of a variable in Java.
Use getClass().getSimpleName() to Check the Type of a Variable in Java
We can check the type of a variable in Java by calling getClass().getSimpleName() method via the variable. The below example illustrates the use of this function on non-primitive data types like String .
public class MyClass public static void main(String args[]) String str = "Sample String"; System.out.println(str.getClass().getSimpleName()); > >
The below example illustrates the use of this method on an array.
public class MyClass public static void main(String args[]) String[] arr = new String[5]; System.out.println(arr.getClass().getSimpleName()); > >
This method is callable by objects only; therefore, to check the type of primitive data types, we need to cast the primitive to Object first. The below example illustrates how to use this function to check the type of non-primitive data types.
public class MyClass public static void main(String args[]) int x = 5; System.out.println(((Object)x).getClass().getSimpleName()); > >