Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Semrush - keyword research tool

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Teach Your Child To Read

Disclosure: My content contains affiliate links.

31,647 questions

41,700 answers

573 users

How to check the data type of a variable in Java

1 Answer

0 votes
class VarType_Java {
    public static void main(String[] args) {
        Object o1 = "Java";
        
        if (o1 instanceof String) {
            System.out.println("o1 is a String");
        }

        Object o2 = 9320;
        String dataType = o2.getClass().getName();
        System.out.println("o2 is " + dataType);
        
        Object o3 = 3.14;
        dataType = o3.getClass().getName();
        System.out.println("o3 is " + dataType);
        
        int a = 783; 
        System.out.println(((Object)a).getClass().getName());
        
        char b = 'a'; 
        System.out.println(((Object)b).getClass().getName());
        
        double c = 943498.083; 
        System.out.println(((Object)c).getClass().getName());
        
        String d = "abc"; 
        System.out.println(((Object)d).getClass().getName());
        
        boolean e = true; 
        System.out.println(((Object)e).getClass().getName());
    }
}



/*
run:

o1 is a String
o2 is java.lang.Integer
o3 is java.lang.Double
java.lang.Integer
java.lang.Character
java.lang.Double
java.lang.String
java.lang.Boolean

*/

 



Learn & Practice Python
with the most comprehensive set of 13 hands-on online Python courses
Start now


answered Jul 7 by avibootz

Related questions

1 answer 5 views
1 answer 6 views
1 answer 7 views
1 answer 6 views
1 answer 6 views
...