How to Add Two Numbers in Java
Add two numbers in Java with int, long, double, and BigDecimal, including user-entered input.
How to Add Two Numbers in Java
Adding two numbers is the first arithmetic most people write in Java, but the "right" way depends on where the numbers come from and how large they can get. This chapter covers the idiomatic approaches: literal int math, parsing values from text, floating-point addition, and adding safely without silent overflow.
Adding two int values
The simplest case is adding two int variables with the + operator. The result is itself an int.
int a = 7;
int b = 5;
int sum = a + b;
System.out.println(sum); // 12This is the everyday case and the one you will write most. The only thing to watch for is the range of int: it holds values from -2,147,483,648 to 2,147,483,647. Add past that limit and the value silently wraps around instead of throwing — covered below.
Adding numbers parsed from text
When the numbers arrive as strings — from console input, a file, or an HTTP request — you must convert them before adding. Use Integer.parseInt for whole numbers or Double.parseDouble for decimals.
String first = "42";
String second = "58";
int sum = Integer.parseInt(first) + Integer.parseInt(second);
System.out.println(sum); // 100A common beginner mistake is using + directly on the strings: "42" + "58" produces "4258" because + concatenates strings rather than adding them. Parse first, then add. If the text is not a valid number, parseInt throws NumberFormatException, so validate or wrap the call in a try/catch when the input is untrusted.
Adding decimals and handling overflow
For decimal values, add double (or float) operands. Be aware that binary floating-point cannot represent every decimal exactly, so 0.1 + 0.2 is 0.30000000000000004, not 0.3. For money, use BigDecimal instead.
For whole numbers that may exceed the int range, widen one operand to long, or use Math.addExact to turn overflow into an exception rather than a wrong answer.
| Approach | Behavior on overflow | Use when |
|---|---|---|
int + int | Silently wraps around | Values comfortably within int range |
(long) a + b | Computes in 64-bit, no wrap | Sum may exceed int but fits in long |
Math.addExact(a, b) | Throws ArithmeticException | You must detect overflow, not absorb it |
BigInteger / BigDecimal | Arbitrary precision | Values can be arbitrarily large or need exact decimals |
int big = Integer.MAX_VALUE;
System.out.println(big + 1); // -2147483648 (wrapped!)
System.out.println((long) big + 1); // 2147483648 (correct)
System.out.println(Math.addExact(big, 1)); // throws ArithmeticExceptionA worked example
What to take from the run:
int sum: 12shows the plain+operator adding twointvalues directly — the everyday case.parsed sum: 100confirms thatInteger.parseIntturns"42"and"58"into numbers before adding, rather than concatenating them into"4258".double sum: 0.30000000000000004is the floating-point representation error from0.1 + 0.2— proof thatdoubleis not exact for decimals, so reach forBigDecimalwhen precision matters.int overflow: -2147483648showsInteger.MAX_VALUE + 1wrapping silently to the most negativeint, whilelong safe: 2147483648gives the correct answer by widening one operand tolong.addExact: overflow detectedconfirms thatMath.addExactthrowsArithmeticExceptionon the same overflow instead of returning a wrong value.
Practice
Why does Integer.parseInt('42') + Integer.parseInt('58') give 100 while '42' + '58' gives '4258'?