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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:13:34+00:00 2026-05-25T06:13:34+00:00

Can anyone confirm if this is true? Java will turn a long % int

  • 0

Can anyone confirm if this is true?

Java will turn a long % int into a long value. However it can never be greater than the modulus to it is always safe to cast it to an int.

long a = 
int b =
int c = (int) (a % b); // cast is always safe.

Similarly a long % short will always be safe to cast to a short.

If true, does any one know why Java has a longer type for % than needed?

Additionally, there is a similar case for long & int (if you ignore sign extension)

  • 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-25T06:13:34+00:00Added an answer on May 25, 2026 at 6:13 am

    For most (if not all) arithmetic operations, Java will assume you want the maximum defined precision. Imagine if you did this:

    long a = ...;
    int b = ...;
    
    long c = a % b + Integer.MAX_VALUE;
    

    If Java automatically down-casted a % b to an int, then the above code would cause an int overflow rather than setting c to a perfectly reasonable long value.

    This is the same reason that performing operations with a double and an int will produce a double. It’s much safer to up-cast the least-accurate value to a more accurate one. Then if the programmer knows more than the compiler and wants to down-cast, he can do it explicitly.

    Update

    Also, after thinking more about this, I’m guessing most CPU architectures don’t have operations that combine 32-bit and 64-bit values. So the 32-bit value would need to be promoted to a 64-bit value just to use it as an argument to the CPU’s mod operation, and the result of that operation would be a 64-bit value natively. A down-cast to an int would add an operation, which has performance implications. Combining that fact with the idea that you might actually want to keep a long value for other operations (as I mention above), it really wouldn’t make sense to force the result into an int unless the developer explicitly wants it to be one.

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

Sidebar

Related Questions

Can anyone confirm that after changing the Apple Push Certificate to follow the new
Can anyone confirm if there's an access 2003 runtime available to allow users to
My Get confirm isn't triggering the query? Can anyone see what I'm doing wrong?
Can anyone let me know how can we change the value of kendo combobox
Can anyone explain to me why this program: for(float i = -1; i <
Can anyone help - this is driving me mad. I am calling a mysql
Can anyone explain why here a = [] ? 1 : 2 a will
Can anyone tell me why this code would not be working? $('body').on('test', function() {
can anyone help me with this code. Everything is working fine except this piece
Can anyone enlighten me to a way I can Highlight the content of an

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.