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

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

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

Semrush - keyword research tool

Linux Foundation Training and Certification

Disclosure: My content contains affiliate links.

33,433 questions

43,814 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 47 views
1 answer 36 views
1 answer 47 views
1 answer 50 views
1 answer 41 views
...