Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I know if Object is String type object?

Tags:

java

I have to know if Object is String or any other class type, how can I do it? Currently I do it like below, but its not very good coding.

try {     String myString = (String) object;     // do stuff here } catch(Exception e) {     // it wasn't string, so just continue } 
like image 679
newbie Avatar asked Dec 03 '10 11:12

newbie


People also ask

How do you check whether an object is a string?

The best way to checks if the object is a string is by using the isinstance() method in Python. This function returns True if the given object is an instance of class classified or any subclass of class info, otherwise returns False.

How do you check what type an object is?

Use the typeof operator to get the type of an object or variable in JavaScript. The typeof operator also returns the object type created with the "new" keyword. As you can see in the above example, the typeof operator returns different types for a literal string and a string object.

How do you check if a Java object is a string?

The Java instanceof keyword is used to check if an object is a certain type. It returns true or false. For example, we can check if a variable is a type of String; we can test classes to see if they are certain types (e.g., is a Birch a Tree or a BoysName?).

How to check if an object is string or not in JavaScript?

There are different ways in JavaScript to check if an object is string or not in JavaScript. This post will show you couple of different ways with examples. The typeof operator can be used to check the type of an operand. If we use it with a string variable, it will return “string”.

How to check if string is literal type or object type?

In the below example, we are going to define the string in a literal form and an object form. Once the form is defined, we will be using the typeof or instanceof method for checking whether the string is of literal type or object type.

How to find the type of an object using a function?

To use it, you just need to pass the object through the function as a parameter and you'll quickly get your answer. For example, if you're looking to find the type of an object that looks like this: one = ['purple', 'yellow', 'green'] You'll just need to use the type() function, like this: type(one)

What is an object in JavaScript?

In most languages, values are denoted by Integers, floating-point numbers, strings, boolean, characters, arrays, records, and many more. On the other hand, a JavaScript object can be defined as a list of unordered primitive data types (and sometimes reference data types) that are stored as a pair with key and value.


2 Answers

 object instanceof Type 

is true if the object is a Type or a subclass of Type

 object.getClass().equals(Type.class)  

is true only if the object is a Type

like image 91
Andreas Dolk Avatar answered Sep 28 '22 01:09

Andreas Dolk


Use the instanceof syntax.

Like so:

Object foo = "";  if( foo instanceof String ) {   // do something String related to foo } 
like image 40
javamonkey79 Avatar answered Sep 28 '22 01:09

javamonkey79