typeof in Java 8

We Are Going To Discuss About typeof in Java 8. So lets Start this Java Article.

typeof in Java 8

  1. typeof in Java 8

    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.

  2. typeof in Java 8

    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.

Solution 1

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.

Original Author Of This Content

Solution 2

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

Original Author edited Jun 20, 2020 at 9:12 Of This Content

Solution 3

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
}

}

Original Author Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment