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

The Archive Base Latest Questions

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

there is something puzzling me and I did not find much information on the

  • 0

there is something puzzling me and I did not find much information on the VM specs. It’s a bit obscure and that’d be nice if someone could explain me.

These few lines of code…..

double myTest = Double.MAX_VALUE;

System.out.println("1. float: " + (float)myTest);
System.out.println("2. int: " + (int)myTest);
System.out.println("3. short: " + (short)myTest);
System.out.println("4. byte: " + (byte)myTest);

….. produce this output:

  1. float: Infinity
  2. int: 2147483647
  3. short: -1
  4. byte: -1

byte, short and int are 8, 16, 32 bit with two’s complement. float and double are 32 and 64 bit IEEE 754 (see here).

From my understanding, the max value of a double implies that all the bits of the mantisse (52 bits) are switched to 1. Therefore it’s not (very) surprising that a cast to short or to byte returns -1 i.e all bits are switched to 1. It seems that the cast keeps the ‘tail’ of the double so that it fits into 8 bit byte or 16 bit short.

What surprises me is the cast to int and, to a lesser extent, the cast to float.
How is it possible to get “2. int: 2147483647” which is 0x7FFFFFFF, the maximal value while short and byte 3. and 4. are -1 ?

The cast to float is also weird. If the 32 bits at the ‘tail’ of myTest were kept, then shouldn’t it generate a NaN ?

  • 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-03T09:40:05+00:00Added an answer on June 3, 2026 at 9:40 am

    JLS spells out the rules in section 5.1.3 Narrowing Primitive Conversion. The rules depend on the target type.

    float:

    A narrowing primitive conversion from double to float is governed by the IEEE 754 rounding rules (§4.2.4). This conversion can lose precision, but also lose range, resulting in a float zero from a nonzero double and a float infinity from a finite double. A double NaN is converted to a float NaN and a double infinity is converted to the same-signed float infinity.

    int and long:

    one of the following two cases must be true:

    • …
    • The value must be too large (a positive value of large magnitude or positive infinity), and the result of the first step is the largest representable value of type int or long.

    byte, char and short:

    If the target type is byte, char or short, the conversion it two-step. First, the double is converted to long as explained above. Then, the long is converted to the final type as follows:

    A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the sign of the resulting value to differ from the sign of the input value.

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

Sidebar

Related Questions

Is there something in the Android developer guidelines that disuades developers from providing the
I am studying COM so there're some basic questions puzzling me... I know that
Is there something similar to java's InvocationHandler in vala. That is, I want to
Is there something like python's interactive REPL mode, but for Java? So that I
Is there something similar to Stackless Python , i.e. a framework that supports continuations,
I'm just getting into TDD with Rails. Something that is puzzling me is when
Is there something like InstallShield that I can use for free?
Is there something like a panel that I can use in a MFC application.
This is just something that's been puzzling me ever since I read about iterators
Is there something in EF 4.1 that allows for a reconnect to a database

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.