Ad
  • Custom User Avatar

    It looks like it's a test to make sure that multiplying by two doesn't exceed the maximum or minimum value that an integer can hold. That is why the value is stored in a long which can hold a larger number, and then if it is greater or less than what an integer can store, it returns the maximum or minimum value that it can hold. If you have a 32 bit int, the values have to be within the range -2,147,483,648 to 2,147,483,647.

    For example, if we pass a large number into the function like so: "doubleInteger(2111222333)" we can't just multiply that number by two (ie, return i * 2) because that number doubled is 4222444666. A method that simply calls "return i * 2" would actually give a result of -72522630 because as the number goes past the upper range of 2,147,483,647 it overflows and and adds to remaining value to the minimum value an integer can hold (so -2,147,483,648 + (4,222,444,666 - 2,147,483,647) = -72,522,630).

    Here's an article for reference on overflowing variables: https://www.baeldung.com/java-overflow-underflow