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 6603953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:05:11+00:00 2026-05-25T19:05:11+00:00

A few related questions here. As per the title, why is it a requirement

  • 0

A few related questions here.

As per the title, why is it a requirement if we are specifying the variable type as long or float, double? Doesn’t the compiler evaluate the variable’s type at compile time?

Java considers all integral literals as int – is this to lessen the blow of inadvertent memory waste? And all floating-point literals as double – to ensure highest precision?

  • 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-25T19:05:12+00:00Added an answer on May 25, 2026 at 7:05 pm

    When you have a constant there are subtle differences between value which look the same, but are not. Additionally, since autoboxing was introduce, you get a very different result as less.

    Consider what you get if you multiply 0.1 by 0.1 as a float or as a double and convert to a float.

    float a = (float) (0.1 * 0.1);
    float b = 0.1f * 0.1f;
    System.out.println("a= "+new BigDecimal(a));
    System.out.println("b= "+new BigDecimal(b));
    System.out.println("a == b is " + (a == b));
    

    prints

    a= 0.00999999977648258209228515625
    b= 0.010000000707805156707763671875
    a == b is false
    

    Now compare what you get if you use either float or int to perform a calculation.

    float a = 33333333f - 11111111f;
    float b = 33333333 - 11111111;
    System.out.println("a= "+new BigDecimal(a));
    System.out.println("b= "+new BigDecimal(b));
    System.out.println("a == b is " + (a == b));
    

    prints

    a= 22222220
    b= 22222222
    a == b is false
    

    Compare int and long

    long a = 33333333 * 11111111; // overflows
    long b = 33333333L * 11111111L;
    System.out.println("a= "+new BigDecimal(a));
    System.out.println("b= "+new BigDecimal(b));
    System.out.println("a == b is " + (a == b));
    

    prints

    a= -1846840301
    b= 370370362962963
    a == b is false
    

    compare double with long

    double a = 333333333333333333L  / 333333333L;
    double b = 333333333333333333D  / 333333333D;
    System.out.println("a= "+new BigDecimal(a));
    System.out.println("b= "+new BigDecimal(b));
    System.out.println("a == b is " + (a == b));
    

    prints

    a= 1000000001
    b= 1000000000.99999988079071044921875
    a == b is false
    

    In summary its possible to construct a situation where using int, long, double or float will produce a different result compared with using another type.

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

Sidebar

Related Questions

I've seen a few related questions out here, but they don’t exactly talk about
Ok, I've asked a few related questions here and only ended up with more
I found a few questions related to this around here but not exactly what
I've seen a few questions here related to determining the similarity of files, but
I have a few questions related to Windows processes in kernel and usermode. If
I've read a few related questions, but none of them really helps me. So,
I had put up a few questions related but i wasn't clear enough. I
I have tried solving this problem by posting other related questions here that focused
I saw only a few questions related BlackBerry, but I think knowledgeable members are
There are some related questions here and here , but they didn't really give

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.