0

So I was doing a question on leetcode in which an integer was in an array form with its digit in left to right.

I wrote the following code to convert the input array into integer. But when 9999999999 i.e 10 9's or more are given as array. It is giving garbage value after adding 9th 9. The code is given below. Please help me clear this doubt as I am just a beginner and I really cannot understand it. Sorry if its a silly doubt.

public class YourClassNameHere{
    public static void main(String[] args){
        int number = 0;
        int[] num = {9,9,9,9,9,9,9,9,9,9,9};
        for(int i=0; i<num.length; i++){
            number = number*10 + num[i];
        }
        System.out.println(number);
    }
}

I expected number = 9999999999

But I got number = 1410065407

1
  • 3
    Suggested area of research - what's the range of valid values for an int?
    – Jon Skeet
    Commented Jun 11, 2023 at 7:03

2 Answers 2

2

The maximum value for int in java is 2147483647

9999999999 is much bigger therefore it's causing overflow and you're receiving 'weird' value

Every variable, not only in java, is technically just a list of bits and, due to the binary numbers definition, it's encoding some 'power of 2' value - depends on the length of a list. int consist of 32 bits and the first one is for 'plus/minus' and also we're counting them from 0 then max int value is (2^30 + 2^29 + 2^28 + ... + 2^0) = 2147483647 - all bits being set to 1

You should use long instead here and respect it's range - you can exceed it too anyway


Read also:

1
  • 2
    It's worth to add that if there is need to have even bigger numbers than long can afford - there is always a possibility to use BigInteger or BigDecimal for floating point numbers
    – dey
    Commented Jun 11, 2023 at 7:31
0

As you know, all values within a computer are stored in as binary numbers, which are referred to as bits.

A bit holds a 0, or a 1.  And, a byte is, usually, 8 bits.

And, an int value, in Java, occupies 32 bits—or 4 bytes—of memory.

For example here is 9, in binary.

00000000 00000000 00000000 00001001

In order to store a negative value, computers utilize the empty space to the left.
So, here is a −9 in binary.

11111111 11111111 11111111 11110111

This means that the largest value an int can contain is 2,147,483,647.

01111111 11111111 11111111 11111111

And, the lowest number, would be −2,147,483,648.

10000000 00000000 00000000 00000000

Thus, you'll find that on iteration 10 of your for-loop, the maximum value is surpassed, and "overloaded".

i =  0, number =          9,                             1001
i =  1, number =         99,                          1100011
i =  2, number =        999,                       1111100111
i =  3, number =       9999,                   10011100001111
i =  4, number =      99999,                11000011010011111
i =  5, number =     999999,             11110100001000111111
i =  6, number =    9999999,         100110001001011001111111
i =  7, number =   99999999,      101111101011110000011111111
i =  8, number =  999999999,   111011100110101100100111111111
i =  9, number = 1410065407,  1010100000010111110001111111111
i = 10, number = 1215752191,  1001000011101101110011111111111
1215752191

Here is the modified code, to produce the output.

int number = 0;
int[] num = {9,9,9,9,9,9,9,9,9,9,9};
for(int i=0; i<num.length; i++){
    System.out.printf("i = %2d, ", i);
    number = number*10 + num[i];
    System.out.printf("number = %10d, ", number);
    System.out.printf("%32s%n", Integer.toBinaryString(number));
}
System.out.println(number);

Java, as with many other coding languages, offers a 64 bit integer value, a long.

This will contain the 11 digits you're looking for.
Simply, change number to a long data-type.

long number = 0;

Output

i =  0, number =               9,                                     1001
i =  1, number =              99,                                  1100011
i =  2, number =             999,                               1111100111
i =  3, number =            9999,                           10011100001111
i =  4, number =           99999,                        11000011010011111
i =  5, number =          999999,                     11110100001000111111
i =  6, number =         9999999,                 100110001001011001111111
i =  7, number =        99999999,              101111101011110000011111111
i =  8, number =       999999999,           111011100110101100100111111111
i =  9, number =      9999999999,       1001010100000010111110001111111111
i = 10, number =     99999999999,    1011101001000011101101110011111111111
99999999999

As a final note, Java offers the BigInteger, and BigDecimal classes, which will allow you to compute values without a constraint.

BigInteger number = BigInteger.ZERO;
int[] num = {9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9};
BigInteger numB;
for(int i=0; i<num.length; i++){
    numB = new BigInteger(String.valueOf(num[i]));
    number = number.multiply(BigInteger.TEN).add(numB);
}
System.out.println(number);

Output

999999999999999999999999999999999

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.