Here I would like to mention the concept of integer clock.

The maximum and minimum values for int in Java are:

int MAX_VALUE = 2147483647
int MIN_VALUE = -2147483648

Please check the following results

 int a = 2147483645;
 for(int i=0; i<10; i++) {
    System.out.println("a:" + a++);
 }

Output:

a:2147483645
a:2147483646
a:2147483647
a:-2147483648
a:-2147483647
a:-2147483646
a:-2147483645
a:-2147483644
a:-2147483643
a:-2147483642

It shows that when you go beyond the limit of the +ve range of integer, the next values starts from its negative starting value again.

 -2147483648,       <-----------------
 -2147483647,                        |
 -2147483646,                        |
  .                                  |
  .                                  |
  .                                  |    (the next value will go back in -ve range)
  0,                                 |
 +1,                                 |
 +2,                                 |
 +3,                                 |
  .                                  |
  .                                  |
  .,                                 |
 +2147483645,                        |
 +2147483646,                        |
 +2147483647     ---------------------

If you calculate the factorial of 13 it is 6227020800. This value goes beyond the int range of java. So the new value will be

        6227020800
      - 2147483647 (+ve max value)
   -----------------
Value = 4079537153
      - 2147483648 (-ve max value)
   -----------------
value = 1932053505
   -             1  (for zero in between -ve to +ve value)
  ----------------
Answer = 1932053504

So, in your answer, the factorial of 13 is becoming 1932053504. This is how integer clock works.

You can use long datatype instead of integer to achieve your purpose.

Answer from Jay on Stack Overflow
Top answer
1 of 5
27

Here I would like to mention the concept of integer clock.

The maximum and minimum values for int in Java are:

int MAX_VALUE = 2147483647
int MIN_VALUE = -2147483648

Please check the following results

 int a = 2147483645;
 for(int i=0; i<10; i++) {
    System.out.println("a:" + a++);
 }

Output:

a:2147483645
a:2147483646
a:2147483647
a:-2147483648
a:-2147483647
a:-2147483646
a:-2147483645
a:-2147483644
a:-2147483643
a:-2147483642

It shows that when you go beyond the limit of the +ve range of integer, the next values starts from its negative starting value again.

 -2147483648,       <-----------------
 -2147483647,                        |
 -2147483646,                        |
  .                                  |
  .                                  |
  .                                  |    (the next value will go back in -ve range)
  0,                                 |
 +1,                                 |
 +2,                                 |
 +3,                                 |
  .                                  |
  .                                  |
  .,                                 |
 +2147483645,                        |
 +2147483646,                        |
 +2147483647     ---------------------

If you calculate the factorial of 13 it is 6227020800. This value goes beyond the int range of java. So the new value will be

        6227020800
      - 2147483647 (+ve max value)
   -----------------
Value = 4079537153
      - 2147483648 (-ve max value)
   -----------------
value = 1932053505
   -             1  (for zero in between -ve to +ve value)
  ----------------
Answer = 1932053504

So, in your answer, the factorial of 13 is becoming 1932053504. This is how integer clock works.

You can use long datatype instead of integer to achieve your purpose.

2 of 5
3

Please run this code:

System.out.println("Minimum value of Integer is: " + Integer.MIN_VALUE);
System.out.println("Maximum value of Integer is: " + Integer.MAX_VALUE);

So you can see why it fails.

🌐
Baeldung
baeldung.com › home › java › java numbers › how to check whether an integer exists in a range with java
How to Check Whether an Integer Exists in a Range with Java | Baeldung
January 17, 2024 - For example, suppose we wanted to know whether the integer 20 occurs within these two ranges: R1 = [10, 2o), a left-closed right-open range, and R2 = (10, 20], a left-open right-closed range. Since R1 does not contain its upper bound, the integer 20 exists only in R2. Our goal is to determine whether a number is between a given lower and upper bound. We’ll start by checking for this using basic Java operators.
🌐
Reddit
reddit.com › r/learnprogramming › what is the best way to store an integer range in java?
r/learnprogramming on Reddit: What is the best way to store an integer range in java?
August 22, 2017 -

I am making a math workout program and I need to prompt the user to input what digit range would he want in his math problem (using numbers from 0 to 9, from 10 to 100...).

I thought about making an int array (say I want addition problems that user numbers no bigger than 100 and no smaller than 0, id write int[] range = {0, 100}).

Then id use that array in generating numbers for the problem.

Is this the best way?

Top answer
1 of 2
5
Just store the min and max in separate int variables. That's the easiest approach. To generate the numbers to solve all you need to do is generate a random number in the range. Here, you can use the .nextInt() method from the Random class: Random rng = new Random(); // create a new Random generator instance int numberInRange = rng.nextInt((max + 1) - min) + min; The line int numberInRange = rng.nextInt((max + 1) - min) + min; generates a random number in the range min to max (inclusive). The parameter of the .nextInt() method is the maximum range excluded, so .nextInt(10) will generate random numbers in the range 0 to 9. By adding 1 to max I include max in the range, and then I subtract the min to get the range of possible numbers, that then need to be offset by adding min. As a concrete example: your range 10 to 100 inclusive Random rng = new Random(); // create a new Random generator instance int min = 10; int max = 100; int numberInRange = rng.nextInt((max + 1) - min) + min; When you calculate: max + 1, you get 101. Were you using this value as upper boundary for the .nextInt method, you'd get numbers in the range 0 to 100 inclusive. since you want 10 to 100 inclusive, you only have 91 distinct numbers, so you need to subtract the min (10) from the above result, 101 - 10 yields 91 Now, the .nextInt method produces numbers in the range 0 to 90 inclusive Last, by adding min to the random number generated above, you offset the range so that it goes from 10 to 100. Edit: optimally, you'd move the random number in range generation into its own method. My approach would be as follows: At class level (as a field): static Random rng = new Random(); The method itself: public static int rangedRandomInt(int min, int max) { return rng.nextInt((max + 1) - min) + min; } Or, with late initialisation At class level (as a field): static Random rng; The method itself: public static int rangedRandomInt(int min, int max) { if(rng == null) { // on-demand initialization of Random number generator rng = new Random(); } return rng.nextInt((max + 1) - min) + min; }
2 of 2
2
I agree with u/desrtfx . When you use an array (or some separate Range class) you’re creating a reference to a completely separate object, and one whose length is effectively unknown until the JRE starts optimizing. When you use two separate variables, their values are embedded directly in the body of their class or stack frame, and the compiler/JRE can see everything except the eventual run-time value beforehand. If you need to pass ranges around between methods or return them from a public method, you’ll either have to take two separate arguments as int min, int max or bite the bullet and come up with some class Range to bundle them up. You can also bundle two ints up into a long for internal passing-around, should the need arise: long bundle = (min & 0xFFFFFFFFL) | ((long)max << 32); int bundle_min = (int)bundle; int bundle_max = (int)(bundle >>> 32); In general—and Java makes this sort of hard in some places—it’s best to tie down your implementation as much as possible type-wise, so that neither you, nor users, nor other programmers can easily break things (e.g., by passing new int[0]). The types and variable names then serve as additional documentation, clarifying what the int values actually mean to the program.
🌐
Medium
medium.com › @AlexanderObregon › checking-if-a-number-is-within-a-range-in-java-da2729ea3e05
Checking if a Number Is Within a Range in Java | Medium
August 25, 2025 - This control is what lets Java range checks adapt to a wide set of contexts, such as validating user input, setting thresholds, or evaluating loop conditions. It’s also possible to combine inclusive and exclusive checks in a single condition when the problem requires it. A grading system might give full marks at 100 but exclude zero as a passing value. int score = 0; boolean passing = score > 0 && score <= 100; System.out.println(passing); // false
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Adding Range Type in Java - Java Code Geeks
July 26, 2020 - Programming languages such as Python, ... a verbose, wordy programming language [Pras 2012] by nature. But yet, there is no range type in the Java language....
🌐
IBM
ibm.com › docs › en › i › 7.4.0
COBOL and Java Data Types
We cannot provide a description for this page right now
🌐
GeeksforGeeks
geeksforgeeks.org › java › intstream-range-java
IntStream range() in Java - GeeksforGeeks
December 6, 2018 - IntStream range(int startInclusive, int endExclusive) returns a sequential ordered IntStream from startInclusive (inclusive) to endExclusive (exclusive) by an incremental step of 1. Syntax : static IntStream range(int startInclusive, int ...
🌐
W3Schools
w3schools.com › java › java_data_types.asp
Java Data Types
assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof int interface long module native new package private protected public return requires short static super switch synchronized this throw throws transient try var void volatile while Java String Methods
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › datatypes.html
Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
Integer literals can be expressed by these number systems: Decimal: Base 10, whose digits consists of the numbers 0 through 9; this is the number system you use every day · Hexadecimal: Base 16, whose digits consist of the numbers 0 through 9 and the letters A through F · Binary: Base 2, ...
🌐
TutorialsPoint
tutorialspoint.com › intstream-range-method-in-java
IntStream range() method in Java
The range() method in the IntStream class in Java is used to return a sequential ordered IntStream from startInclusive to endExclusive by an incremental step of 1. This includes the startInclusive as well. The syntax is as follows −
🌐
LabEx
labex.io › tutorials › java-how-to-handle-large-integer-ranges-in-java-515581
How to handle large integer ranges in Java | LabEx
BigInteger is a Java class in the java.math package designed to handle arbitrarily large integers without precision limitations. It provides a robust solution for numeric computations beyond primitive type ranges.
🌐
Educative
educative.io › answers › what-is-rangebetween-in-java
What is Range.between() in Java?
In the first example, we get the Range object for the integer values 100 and 200.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › java tutorial › range in java
Range in Java | How does Range Function work in Java | Examples
June 14, 2023 - In the IntStream class, this method enables the return of sequentially ordered values within the specified range as function parameters. The two parameters used are startInclusive (inclusive) and endExclusive ...
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Coderanch
coderanch.com › t › 482929 › java › Assign-range-integer-variable
Assign a range to an integer variable [Solved] (Java in General forum at Coderanch)
Sanjeev Mehta wrote: I want this to return me i as 255 and not 260. Is there some way I can achieve this rounding? Sure, you can design your own type. You introduce a class called RangeInt256 or something and give it properties you want.
🌐
Coderanch
coderanch.com › t › 658900 › java › Array-range-numbers
Array range of numbers (Beginning Java forum at Coderanch)
It would seem, however, that you haven't read thoroughly enough, because what you wrote will NOT return a range that includes both 0 and 1000. I suggest you look over it again. Carefully. My reason for writing it was so that you could learn from it; not parrot it. Winston · "Leadership is nature's way of removing morons from the productive flow" - Dogbert Articles by Winston can be found here ... Ok, I know I'm coming late into the piece, but you do know to use square brackets with arrays right?
🌐
Oracle
docs.oracle.com › cd › E17802_01 › products › products › java-media › jai › forDevelopers › jai-apidocs › javax › media › jai › util › Range.html
Java Advanced Imaging: Class Range
A range is defined to contain all the values between the minimum and maximum values, where the minimum/maximum value can be considered either included or excluded from the range. This example creates a range of Integers whose minimum value is 1 and the maximum value is 5.
🌐
Baeldung
baeldung.com › home › java › java numbers › listing numbers within a range in java
Listing Numbers Within a Range in Java | Baeldung
January 8, 2024 - But these can also be overused and fall into some common pitfalls. To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams: ... Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.