How to Compute the Sum of Integers in Java
The Java programming language provides a variety of classes and types for modeling numbers. Primitive types include "ints" for integers, while the Integer class provides the ability to model an integer value as an object. Once you have integer values and variables within your Java program, you can carry out computation and arithmetic operations on them, including addition. Java programs can also store the sum value resulting from adding two integers in another variable for further use.
Instructions
-
-
1
Create two primitive type integer variables in your Java program. Use the following syntax to create two variables and instantiate them with integer values:
int firstInt = 5;
int secondInt = 7;
Choose any variable names you like for your integers, but your program will be easier to read and work with if you choose meaningful names. Enter whatever numerical integer values you like in the assignment portions of the two variable declarations.
-
2
Add your two integer variables. The following sample code demonstrates adding the two variables:
firstInt + secondInt
Depending on what you plan on doing with the resulting integer sum value, you may wish to store it in a variable as follows:
int sumInt = firstInt + secondInt;
Your program can now refer to this value as in the following example code:
System.out.println("Sum: " + sumInt);
-
-
3
Create two Integer objects in your program. Instead of using primitive type integers, you may wish to create Integer objects, as in the following example:
Integer firstInteger = new Integer(3);
Integer secondInteger = Integer.valueOf(4);
This code demonstrates two possible methods for creating an Integer object. Give the Integer class the integer value you wish to store within the object, by passing it as a parameter to either the constructor or the "valueOf" method. Alter the variable names and numerical parameters to suit the needs of your program.
-
4
Add your two Integers. The process of adding the values of Integer objects is slightly more complex than with primitives. You can call the methods of the Integer class on any object of the class. The following sample code demonstrates adding the two Integer objects:
Integer sumInteger = Integer.valueOf(firstInteger.intValue() + secondInteger.intValue());
When this code executes, Java will first compute the "valueOf" methods to acquire the numerical values stored within each object, then perform the addition operation on these, before passing the resulting sum to the Integer class, creating the new variable.
-
5
Save your program files, compile and test your program. You can test the sum of the Integer operation using the standard output console:
System.out.println("Sum Integer: " + sumInteger.intValue());
Experiment with the methods of the Integer class to familiarize yourself with it. To get used to carrying out arithmetic calculations using your primitive type integers, you can also use other operators for subtraction, multiplication and division.
-
1
Tips & Warnings
You can carry out addition on primitive type integers without actually storing them as variables, by representing them numerically within your code.
If you do not need specific methods provided by the Integer class, it's best to use primitive types, as these use less memory resources.
References
Resources
- Photo Credit Jupiterimages/Photos.com/Getty Images