typeof in Java 8
Asked Answered
A

3

19

If we want to check the datatype of variable in javascript, we can use typeof operator .

Consider this snippet

var c = 'str' ;
console.log(typeof(c)); // string
c = 123 ;
console.log(typeof(c)); // number
c =  {} ;
console.log(typeof(c)) ; // object

I want to achieve the same functionality in Java 8 . Java does not have typeof operator but there's the instanceof operator to check the types.

System.out.println("str" instanceof String);  // true 

Integer a  = 12 ;
System.out.println(a instanceof Integer);


Float f = 12.3f
System.out.println(f instanceof Float); // true

Can we do any better ? Plus instanceof does not support primitive types .

Is there any approaches in java 8 ? Any relevant approaches will be appreciated.

Attorneyatlaw answered 8/4, 2020 at 15:47 Comment(7)
Why do you think you need such an operator for primitive types?Indiscrimination
And what do you mean by do any better?Indiscrimination
any methods to check for primitive types also . same as typeof in javascriptAttorneyatlaw
To check it where? You already know that it's an int or char or a short, there's no use for such an operator.Indiscrimination
I want to check that for typecasting and after sucessful execution I want to check its typeAttorneyatlaw
Again, if you're casting, you know the resulting type statically. It can't be different at runtime.Indiscrimination
Also, var is a thing in java, but normally you do not use it, because it creates confusion.Oxley
R
26

You can use the getClass() method to get the type of the object you are using:

Object obj = null;
obj = new ArrayList<String>();
System.out.println(obj.getClass());

obj = "dummy";
System.out.println(obj.getClass());

obj = 4;
System.out.println(obj.getClass());

This will generate the following output:

class java.util.ArrayList
class java.lang.String
class java.lang.Integer

As you see it will show the type of the object which is referenced by the variable, which might not be the same as the type of the variable (Object in this case).

For primitive types there is no solution available as the problem of knowing the type stored in a variable does not exist. A primitive type variable can hold only values of that type. As you have to define the variable (or parameter) somewhere you already know the type of it and the values it can hold. There is no "base" type for primitive values which you can use similar to the Object type, which is the base type for all objects in java.

Reflexion answered 8/4, 2020 at 18:2 Comment(1)
Great approach. that's what I was thinking about .Attorneyatlaw
A
9

Thanks to @Progman for getClass() method .

class check{

    static Class typeof(Integer a)
    {
        return a.getClass();
    }
    static Class typeof(Character c)
    {
        return c.getClass();
    }
    static Class typeof(Float f)
    {
        return f.getClass();
    }
    static Class typeof(Double d)
    {
        return d.getClass();
    }
    static Class typeof(Long l)
    {
        return l.getClass();
    }
    static Class typeof(String s)
    {
        return s.getClass();
    }

}

So now we check both primitive and non- primitive types

check.typeof(12) ; // class java.lang.Integer

check.typeof(12.23f) ; // class java.lang.Float

check.typeof('c') ; // class java.lang.Character

check.typeof("str") ; // class java.lang.String

Attorneyatlaw answered 9/4, 2020 at 7:26 Comment(1)
static Class typeof(Object a) { return a.getClass(); } is simpler and not limited to a type.Continuo
O
4

This is the closest functionality example I could find in Java (10 or higher) to your JavaScript example with the typeof operator and var (local variable declaration only in Java).

It deals with primitive data type and implies casting the primitive to Object first and then calling the .getClass().getSimpleName() method on the respective Object.

References: Use getClass().getSimpleName() to Check the Type of a Variable in Java

public class Main {
public static void main(String[] args) {
    var myNum = 72;
    System.out.println(((Object) myNum).getClass().getSimpleName()); // Integer
    var myInput = 10.52f;
    System.out.println(((Object) myInput).getClass().getSimpleName()); // Float
    var yetAnotherInput = 0.345678923;
    System.out.println(((Object) yetAnotherInput).getClass().getSimpleName()); // Double
    var otherInput = 1_234_567_764_456_211L;
    System.out.println(((Object) otherInput).getClass().getSimpleName()); // Long
    var myName = "John";
    System.out.println(((Object) myName).getClass().getSimpleName()); // String
    var myLetter = 'j';
    System.out.println(((Object) myLetter).getClass().getSimpleName()); // Character
    var myAnswer = true;
    System.out.println(((Object) myAnswer).getClass().getSimpleName()); // Boolean
}

}

Orva answered 6/3, 2022 at 14:27 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Bifrost

© 2022 - 2024 — McMap. All rights reserved.