How to Add Integers in Java
Adding integers in Java is not complicated, and is something you may find yourself doing regularly as part of Java programming. Which approach you take to add integers in Java depends on the details of your own particular program, but none of the main techniques require more than a few lines of code. As with all programming, when performing arithmetic calculations in Java, it's best to consider efficiency as well as getting the result you need.
Instructions
-
-
1
If your integers are not stored as variables within your program, and you simply want to get the result of a calculation, you can "hard code" the numbers as follows:
int answer = 13 + 27; //this stores the result in the variable "answer"
The plus sign is all you need to perform an arithmetic addition; however, although this is the simplest case, it may not always be an advisable approach. With Java programming, the standard or conventional approach is to use variables rather than using numerical values coded directly, as in this example. These are sometimes referred to as "magic numbers," as their chosen values may seem arbitrary to someone who is not familiar with the program.
-
2
If your addition is on primitive type "int" variables, perform it as follows:
//declaration of variables
int firstNumber = 13;
int secondNumber = 27;
int answer = firstNumber + second Number; //store the answer as a variable
Your "int" declarations may have already taken place within your program. You can use the result later in your program by referring to the "answer" variable. If your "int" variables do not have meaningful names -- for example, if they use single letter values such as "a" or "b" -- your program will be harder to read and work with.
-
-
3
If you do not need to store the value of your addition in a variable, you can carry it out within your code as follows:
System.out.println("Answer: " + (firstNumber + secondNumber));
In this case, the value of the answer to the calculation will no longer be available as soon as the line containing it has finished executing. If you need the answer value again later in your code, you will have to carry out the calculation again. However, if you only need the answer on this one line of code, including it without using a variable avoids unnecessary memory usage.
-
4
If your integer values are contained within Java Integer objects, cast these to primitive type "ints" and then add them as follows:
//declaration of Integer objects
Integer firstNumber = new Integer(13);
Integer secondNumber = new Integer(27);
//casting to ints
int answer = firstNumber.intValue() + secondNumber.intValue();
If you prefer, you can store the "int" values of each Integer object as a variable, but if you only need to cast the objects to perform the calculation, there is no need to do this.
-
5
If your integers are stored as other object types such as Strings, use the Integer object to get their values as "ints," as follows:
//declaration of Strings containing integers
String firstNumber = "13";
String secondNumber = "27";
//parse the strings to get the int values and add them
int answer = Integer.parseInt(firstNumber) + Integer.parseInt(secondNumber);
This may happen in cases where your program has been passed parameters from some external source -- for example, from the command prompt. You may opt to store the "int" values of each string as variables, but do not have to if you only need them for the calculation.
-
1
Tips & Warnings
When you choose whether or not to use variables in your programming code, consider both efficiency and readability of your program.
When you carry out casting and parsing operations within your code, there is always a danger that exceptions will be thrown -- for example, if you try to parse something that is not a number.
References
Resources
- Photo Credit Jupiterimages/liquidlibrary/Getty Images