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

  • SEARCH
  • Home
  • 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 917053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:08:53+00:00 2026-05-15T18:08:53+00:00

In Java, all numeric types extend from java.lang.Number. Would it be a good idea

  • 0

In Java, all numeric types extend from java.lang.Number. Would it be a good idea to have a method like the following:

public boolean areEqual(Number first, Number second) {
    if (first != null && second != null) {
        return first.equals(second);
    }
}

I’m concerned about cases where a double 2.00000 does not equal an int 2. Are these handled by the built-in equals? If not, is there any way to write a simple number compare function in java? (external libraries such as apache commons are ok)

  • 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-05-15T18:08:54+00:00Added an answer on May 15, 2026 at 6:08 pm

    A Double is NEVER equals to an Integer. Moreover, a double is not the same as a Double.

    Java has primitive types and reference types. The truly numeric types in Java do not extend from Number, because they’re primitives.

    You may want to consider a system where you’re not mixing types, because that usually will cause a lot of trouble with implicit/explicit conversions that may/may not lose information, etc.

    Related questions

    On int vs Integer:

    • What is the difference between an int and an Integer in Java/C#?
    • Is Java fully object-oriented?

    On Number comparison:

    • Why doesn’t java.lang.Number implement Comparable?
    • Comparing the values of two generic Numbers

    See also

    • Java Language Guide/Autoboxing
    • JLS 4.2 4.2 Primitive Types and Values

      The numeric types are the integral types and the floating-point types.
      The integral types are byte, short, int, and long and char.
      The floating-point types are float and double.


    On mixed-type computation

    Mixed-type computation is the subject of at least 4 puzzles in Java Puzzlers.

    Here are various excerpts:

    it is generally best to avoid mixed-type computations […] because they are inherently confusing […] Nowhere is this more apparent than in conditional expressions. Mixed-type comparisons are always confusing because the system is forced to promote one operand to match the type of the other. The conversion is invisible and may not yield the results that you expect

    Prescription: Avoid computations that mix integral and floating-point types. Prefer integral arithmetic to floating-point.

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

Sidebar

Related Questions

In Java the numeric types all descend from Number so I would use (x
From Effective Java DOCUMENT ALL EXCEPTIONS THROWN BY EACH METHOD It should be noted
Possible Duplicate: USD Currency Formatting in Java All- I have a small problem in
As we all know Java program will start executing from the public static void
Is there a Java package with all the annoying time constants like milliseconds/seconds/minutes in
I have a java class containing all the columns of a database table as
I need an efficient way to read all users from LDAP. I have a
I have some java object and I want to expose all methods of it
Why java Number / Numeric datatypes such as (Integer/Long/...) doesnt throw overflow exception? For
Why java Number / Numeric datatypes such as (Integer/Long/...) doesnt throw overflow exception? For

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.