Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7940853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:29:40+00:00 2026-06-03T23:29:40+00:00

Possible Duplicate: How to determine an object's class (in Java)? Java determine which class

  • 0

Possible Duplicate:
How to determine an object's class (in Java)?
Java determine which class an object is

I have following sample incomplete method to compare the object type of a given object

public void test(Object value) {

        if (value.getClass() == Integer) {
            System.out.println("This is an Integer");
        }else if(value.getClass() == String){
            System.out.println("This is a String");
        }else if(value.getClass() == Float){
            System.out.println("This is a Float");
        }

}

The method can be called as:

test("Test");
test(12);
test(10.5f);

this method is not actually working, please help me to make it work

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:29:42+00:00Added an answer on June 3, 2026 at 11:29 pm

    You forgot the .class:

    if (value.getClass() == Integer.class) {
        System.out.println("This is an Integer");
    } 
    else if (value.getClass() == String.class) {
        System.out.println("This is a String");
    }
    else if (value.getClass() == Float.class) {
        System.out.println("This is a Float");
    }
    

    Note that this kind of code is usually the sign of a poor OO design.

    Also note that comparing the class of an object with a class and using instanceof is not the same thing. For example:

    "foo".getClass() == Object.class
    

    is false, whereas

    "foo" instanceof Object
    

    is true.

    Whether one or the other must be used depends on your requirements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: .NET: Determine the type of this class in its static method How
Possible Duplicate: How to determine the class of a generic type? I have a
Possible Duplicate: How to determine the class of a generic type? How to obtain
Possible Duplicate: How to check if an object is nullable? I have a System.Type
Possible Duplicate: Determine framework (CLR) version of assembly I have a library/DLL file which
Possible Duplicate: Python - Determine the type of an object? I want 'complex' to
Possible Duplicate: jQuery: How to determine which <li> tag was clicked? I have a
Possible Duplicate: How can I compare (directory) paths in C#? I have a filename
Possible Duplicate: In Java, what is the best way to determine the size of
Possible Duplicate: Java how to: Generic Array creation Error generic array creation I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.