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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

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

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,885 questions

51,811 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

*/

 



answered Jul 7, 2024 by avibootz

Related questions

1 answer 133 views
1 answer 105 views
1 answer 120 views
1 answer 138 views
1 answer 110 views
1 answer 228 views
7 answers 417 views
...