Java как проверить переменную

Как проверить тип объекта 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

Splunk tutorial

SPSS tutorial

Swagger tutorial

T-SQL tutorial

Tumblr tutorial

React tutorial

Regex tutorial

Reinforcement learning tutorial

R Programming tutorial

RxJS tutorial

React Native tutorial

Python Design Patterns

Python Pillow tutorial

Python Turtle tutorial

Keras tutorial

Preparation

Aptitude

Logical Reasoning

Verbal Ability

Company Interview Questions

Artificial Intelligence

AWS Tutorial

Selenium tutorial

Cloud Computing

Hadoop tutorial

ReactJS Tutorial

Data Science Tutorial

Angular 7 Tutorial

Blockchain Tutorial

Git Tutorial

Machine Learning Tutorial

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures tutorial

DAA tutorial

Operating System

Computer Network tutorial

Compiler Design tutorial

Computer Organization and Architecture

Discrete Mathematics Tutorial

Ethical Hacking

Computer Graphics Tutorial

Software Engineering

html tutorial

Cyber Security tutorial

Automata Tutorial

C Language tutorial

C++ tutorial

Java tutorial

.Net Framework tutorial

Python tutorial

List of Programs

Control Systems tutorial

Data Mining Tutorial

Data Warehouse Tutorial

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 RSS Feed Subscribe to Get Email Alerts Facebook Page Twitter Page YouTube Blog Page

Источник

Как узнать тип переменной 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

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());   > > 

Related Article — Java Data Type

Источник

Читайте также:  I left my heart on Europa
Оцените статью