-1

I want check the variable type. How do I do that? For e.g

if (num is of String type )
    {This must be executed}

I'm currently using Java 17. Any suggestions?

chornge
  • 2,021
  • 3
  • 28
  • 37

1 Answers1

0

You can use instanceof String to check whether a variable is a String.

if (num instanceof String) {
    // code to be executed
}
George Sun
  • 968
  • 8
  • 21
  • Ok It works Can I check the primitive data types in the same way ? by the way thanks in advance – 17_4011_BALACHANDAR B Sep 25 '21 at 04:33
  • You cannot for primitive data types but there are other ways to check if a value could be valid for that particular data type, e.g. `(x == (int) x)` for `int`, `String.valueOf(b).toLowerCase() == String.valueOf(true) || String.valueOf(b).toLowerCase() == String.valueOf(false)` for `boolean`. – George Sun Sep 25 '21 at 05:41