Java int - How do I add two integers?

Java integer FAQ: How do I add two Java integers (Java int fields)?

Answer: Just use the Java addition operator, the plus sign (+), to add two integers together.

Here's a quick example: If you have to drive 23 miles east, then 14 miles north, what is the total distance you had to drive?

int distanceEast = 23;
int distanceNorth = 14;
int totalDrivingDistance = distanceEast + distanceNorth;

The values 23 and 14 are two Java int (integer) fields, and the totalDrivingDistance will contain the sum of those two values.

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.