W3docs

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); // 12

This 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); // 100

A 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.

ApproachBehavior on overflowUse when
int + intSilently wraps aroundValues comfortably within int range
(long) a + bComputes in 64-bit, no wrapSum may exceed int but fits in long
Math.addExact(a, b)Throws ArithmeticExceptionYou must detect overflow, not absorb it
BigInteger / BigDecimalArbitrary precisionValues 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 ArithmeticException

A worked example

java— editable, runs on the server

What to take from the run:

  • int sum: 12 shows the plain + operator adding two int values directly — the everyday case.
  • parsed sum: 100 confirms that Integer.parseInt turns "42" and "58" into numbers before adding, rather than concatenating them into "4258".
  • double sum: 0.30000000000000004 is the floating-point representation error from 0.1 + 0.2 — proof that double is not exact for decimals, so reach for BigDecimal when precision matters.
  • int overflow: -2147483648 shows Integer.MAX_VALUE + 1 wrapping silently to the most negative int, while long safe: 2147483648 gives the correct answer by widening one operand to long.
  • addExact: overflow detected confirms that Math.addExact throws ArithmeticException on the same overflow instead of returning a wrong value.

Practice

Practice

Why does Integer.parseInt('42') + Integer.parseInt('58') give 100 while '42' + '58' gives '4258'?