Added
A telemetry parser receives decimal digits in a String and must compute their sum without overflowing an int. The input can contain up to 100,000 digits. A team member proposes accumulating by repeatedly concatenating each digit to a StringBuilder and then parsing the result as a long, but the result may exceed long as well. Which implementation best meets the stated need to return the exact sum as an int?
Each character can be converted to its numeric digit by subtracting '0', and summing digit values avoids constructing or parsing the potentially enormous represented number. Math.addExact makes overflow explicit if the exact sum cannot fit the required int return type. Integer.parseInt fails when the whole decimal value exceeds int range, and a long has the same fundamental issue for sufficiently long input. Casting a long sum silently truncates when the sum is outside int range.