How to Add Integers in Java

How to Add Integers in Java thumbnail
Carrying out addition in Java is generally simple.

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.

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.

Related Searches:

References

Resources

  • Photo Credit Jupiterimages/liquidlibrary/Getty Images

Comments

You May Also Like

  • Examples of Reversing Numbers in a Java Program

    Java is a powerful programming language with a wealth of useful classes and methods that simplify tedious tasks and complex data structures....

  • How to Use a Pocket Calculator

    Write a check and know you have the money to pay. Balancing your checkbook after each item you write while you are...

  • How to Add Integers With Exponents

    An exponent represents how many times the base integer is multiplied by itself. For example, "two to the fourth power" is equal...

  • How to Find Integers

    In some computer programming projects, a user or process may provide you with a set of numbers, and you'll need to find...

  • Java 1.5 Enum Tutorial

    The enum type is a new feature in the Java 1.5.0 language, designed to solve weaknesses in the way enumerated values were...

  • How to Add to a C# String Array

    If you declared the array with a number of pre-determined strings instead of typing in a number, then your array will only...

  • How to Change an Integer to a String in Java

    When you enter a number into a text box on a web page, the browser recognizes that number as a string. Computers...

  • How to Add Integers

    Being able to add positive or negative integers together correctly is one of those basic skills that, once you have the hang...

  • How to Compare Integers in Java

    In Java, integer values can be stored and represented in two ways. You can store integers as primitive type "ints" in which...

  • How to Add & Subtract Integers

    An integer is any positive or negative whole number, as well as the number zero. Adding and subtracting integers is a task...

  • How to Create an Immutable Object in Java

    An immutable object in programming, once you've created and initialized it, can never change its state. This may seem counterproductive since the...

  • How to Convert INT to String in Java

    Converting an integer to a string is a common practice when programming. For some application processes, it's necessary to manipulate the format....

  • How to Add Two Large Integers in C++

    C++ is the popular programming language that offers all necessary features including object-oriented programming. C++ supports several types of variables including ...

  • How to Type Special Characters Using Alt Codes

    Sometimes you need to incorporate a special symbol into your writing which is not on the keyboard. In a program such a...

  • How to Leave Comments in Java Code

    Leave short, single-line comments to say something quickly before a small group of lines. Small comments outline what's being done in general...

  • How to Add Integers to Smartboard

    The Smartboard is an interactive whiteboard used for educational and business presentations. The Smartboard setup consists of a freestanding or wall-mounted ...

  • How to Multiply Multiple Integers in C

    The C programming language contains many arithmetic functions that make complex mathematical operations possible. These operations can be performed on numerical data...

Related Ads

Featured