0

I have a string tmp like that: String tmp = "value"; I want to know if the value of tmp is a data type in java or not, in this case not because "value" is different then "int" or "double"... but if tmp hold "int" so it is a data type. I want to know how to check this, if there is an enumeration of data type of java mention it please.

sanyassh
  • 8,100
  • 13
  • 36
  • 70
  • 1
    how would you possible know that? there are millions of people out there creating new datatypes every single hour. you want to check them all? – Stultuske May 17 '19 at 09:47
  • What would it mean to be an "int", as in `String tmp="int"` or `String tmp="1";`? – matt May 17 '19 at 09:48
  • I'm not sure if this is what you're asking, but maybe could help -> https://stackoverflow.com/a/7313605/1984767 – Leviand May 17 '19 at 09:49
  • Why do you want to know this after all? What do you want to achieve in the end? – Roland Illig May 17 '19 at 09:50
  • For primitive types, you have the full list here : https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html – Arnaud May 17 '19 at 09:51
  • You will check if it is data-type and then do what? – Nicholas K May 17 '19 at 09:54
  • If by data-type you mean primitive, then there isn't really a built in list. https://stackoverflow.com/questions/180097/dynamically-find-the-class-that-represents-a-primitive-java-type – matt May 17 '19 at 09:55
  • Possible duplicate of [Dynamically find the class that represents a primitive Java type](https://stackoverflow.com/questions/180097/dynamically-find-the-class-that-represents-a-primitive-java-type) – matt May 17 '19 at 11:03

1 Answers1

0

Java itself doesn't have a built-in list of primitive data types (at least not that I'm aware of), but luckily it's a small and closed list, so you could just create it yourself. For any non-primitive class, you could just attempt to call Class.forName on it:

private static final Set<String> PRIMITIVES = 
    new HashSet<>(
        Arrays.asList("byte", "char", "short", "int", "long", "float", "double", "boolean"));

public static boolean isType(String s) {
    if (PRIMITIVES.contains(s)) {
        return true;
    }

    try {
        Class.forName(s);
    } catch (ClassNotFoundException ignore) {
        retrun false;
    }
    return true;
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350