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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:45:51+00:00 2026-06-11T03:45:51+00:00

This loop will continue indefinitely: char a = 100; for(a=100; a>=0;–a) System.out.println(a); Does it

  • 0

This loop will continue indefinitely:

char a = 100;
for(a=100; a>=0;--a)
    System.out.println(a);

Does it happen because a gets promoted to an int value for arithmetic operations and gets widened to 32 bits from 16 bit char value and hence will always stay positive?

  • 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-11T03:45:52+00:00Added an answer on June 11, 2026 at 3:45 am

    It will indeed loop indefinitely — and the reason you stated is close. It happens because a can’t represent any number that doesn’t satisfy a >= 0 — char is unsigned. Arithmetic underflow is well-defined in Java and unindicated. See the below relevant parts of the specification.

    • §4.2.2

      The integer operators do not indicate overflow or underflow in any way.

      This means there is no indication of overflow/underflow other than just comparing the values… e.g. if a <= --a, then that means an underflow occured.

    • §15.15.2

      Before the subtraction, binary numeric promotion (§5.6.2) is performed on the value 1 and the value of the variable. If necessary, the difference is narrowed by a narrowing primitive conversion (§5.1.3) and/or subjected to boxing conversion (§5.1.7) to the type of the variable before it is stored. The value of the prefix decrement expression is the value of the variable after the new value is stored.

      So, we can see that there are two major steps here: binary numeric promotion, followed by a narrowing pritimive conversion.

    • §5.6.2

      Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:

      • If either operand is of type double, the other is converted to double.
      • Otherwise, if either operand is of type float, the other is converted to float.
      • Otherwise, if either operand is of type long, the other is converted to long.
      • Otherwise, both operands are converted to type int.

      We can see that the decrement expression works with a treated as int, thus performing a widening conversion. This allows it to represent the value -1.

    • §5.1.3

      A narrowing primitive conversion may lose information about the overall magnitude of a numeric value and may also lose precision and range.

      …

      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.

      Keeping only the n lowest order bits means that only the lowest 16 bits of the int expression a - 1 are kept. Since -1 is 0b11111111 11111111 11111111 11111111 here, only the lower 0b11111111 11111111 is saved. Since char is unsigned, all of these bits contribute to the result, giving 65535.

    Noticing something here? Essentially, this all means that Java integer arithmetic is modular; in this case, the modulus is 2^16, or 65536, because char is a 16-bit datatype. -1 (mod 65536) ≡ 65535, so the decrement will wrap back around.

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

Sidebar

Related Questions

What does the second line inside this for loop means and what will be
How can I sort this foreach loop with jQuery? I will sort on id
I need to create a function that uses a loop. This function will open
This loop is strange. I ran this in Java, it gives an Index out
related (sort of) to this question. I have written a script that will loop
This loop checks if a record is in the sqlite database and builds a
This loop works fine in Main function but when copy this loop code in
I have this loop method to display a ViewPager and I set an OnClickListener
How can I vectorize this loop, which populates two square submatrices of a larger
So I do have this loop, but it stops when it come to function

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.