Java Program to Add Two Integers – Learn Calculations

In this tutorial, you’ll learn to store numbers in variables, Adding numbers programmatically, and displaying the results.

After completing this tutorial, you will understand the below Core Java concepts and their Fundamentals-

  1. Creation of Java Variables,
  2. Java Data types,
  3. Java Operators with examples and logic behind the code.

So, let’s begin learning further and develop Java Program to Add Two Integers-


Solution: Java Program to Add Two Integers

public class AddIntegers {

    public static void main(String[] arguments) {
        
        int firstNumber = 100;
        int secondNumber = 200;

        System.out.println("Enter Two Numbers = " + firstNumber+ " " + secondNumber );
        int sum = firstNumber + secondNumber ;                      // <-- Performing Addition...

        System.out.println("The Result of sum = " + sum);
    }
}

 

Output: Once you’ll learn the above program, you will get the following output-

Enter Two Numbers = 100 200
The Result of sum = 300

Logic: Behind The Code

Step 1: Declare the variables

We have used int keyword in the above code. It tells Java, that we are going to create a variable to store the Integer type of data.

If you’re unaware of variables, then you can think of variables as small-small boxes in the memory (RAM) where your data is kept safely to be used further. A variable is a name given to a part of memory (block) to hold our data.

int firstNumber = 100;
int secondNumber = 200;

 

In the above code segment, we’ve created two variables firstNumber and secondNumber to store integers 100 and 200 respectively.

Java Program to Add Two Integers

Step 2: Calculate Addition

Okay, so far we have stored the numbers, now we will add both of them using + addition arithmetic operator. There are other types of operators out there for different purposes in Java, like-

1) Arithmetic Operators:  + –   *   /   %
2) Assignment Operators:  =   +=   -=   *=   /=   %=
3) Relational Operators:   ==   !=   >   <   <=   >=
4) Logical Operators:  &&   ||   !
5) Unary Operators:  + (Unary Plus)   – (Unary Minus)   ++   —
6) Bitwise Operators:  ~   <<   >>   >>>   &   ^

You don’t need to learn all of them right now. As you will learn further Java concepts and practice more and more, you’ll automatically get to know about all of these operators smoothly.


Step 3: Display results

As we have applied the Addition arithmetic operator between firstNumber and SecondNumber, we’ll get the result in the sum variable.


Side note: Operator Precedence and Associativity

In many cases, you will face some expressions having more than one operator like:

int result = 12 - 4 * 2;

 

In this case, What will be the value of the result? Will it be (12 4) * 2, that in total, 16? Or it will be 12 – (4 * 2), that’s equal to, 4?

1) Remember a rule, When two operators, and * in this case, share a common operand, 4 in this case, the operator with the highest precedence will be operated first.

2) In Java programming, the precedence of multiplication operator * is higher than that of subtraction operator  Hence, the multiplication will be performed before subtraction, and the value of result will be 4.

You can learn from the below precedence table, that all binary operators (except for the assignment operators) are evaluated from left to right.

Operators Precedence
postfix expr++ expr--
unary ++expr --expr +expr -expr ~ !
multiplicative * / %
additive + -
shift << >> >>>
relational < > <= >= instanceof
equality == !=
bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
logical OR ||
ternary ? :
assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=
Note***
In Java, only Assignment operators are evaluated right to left.

Side note: Data Types in Java

Data types specify what types of values can be stored in the variable. There are basically two types of data types in Java:

1) Primitive data types: In primitive data types, we have boolean, char, byte, short, int, long, float and double.

2) Non-primitive data types: Classes, Interfaces, and Arrays are part of Non-primitive data types.

In Java programming, primitive data types are the major building blocks to manipulate data. You can think of them as the most basic data types available in Core Java.

Remember***
Java is a statically-typed programming language. It simply means, we can use variables in action statements until it is declared. That’s the reason, why we need to declare variable’s type and name before it’s use.

Till Java v15, there are 8 types of primitive data types:

  • boolean data type
  • byte data type
  • char data type
  • short data type
  • int data type
  • long data type
  • float data type
  • double data type

Here is the diagram to understand Data type Core Java Concepts-

There are 2 catagories of Data types in Core Java- Primitive and Non-Primitive

What Next? 

In this tutorial, we’ve learned and developed a Java program to add two integers. We’ve seen what are the steps and logic behind the code.

We also understood a couple of essential Core Java concepts, such as- Data types, Operator Precedence its Associativity in Java.

To learn other Important Core Java Concepts, Must check these tutorials.

Leave a Comment