Let us now see an example of a java program to add two integers.

In this program, user is asked to enter two integers. Then, the sum of those two integers is stored in a variable and displayed on the screen.

For algorithm & flowchart for addition of two numbers, go through the post given below.

Source Code:

Explanation:

import java.util.Scanner– it imports util package into the code which contains Scanner class in it.

class Addition– The class declaration component declares the name of the class.

public static void main(String args[]) – Java starts running a program with this specific signature

public- is an Access Modifier, which defines who can access this Method. Public means that this Method will be accessible by any Class.

static- is a keyword which identifies the class related thing. This means the given Method or variable is not instance related but Class related.It can be accessed without creating the instance of a Class.

void-  is used to define the Return Type of the Method. Void means the Method will not return any value.

main- is the name of the Method. This Method name is searched by JVM as a starting point for an application with a particular signature only.  String args[] : is the parameter to the main Method.

Scanner scanner = new Scanner(System.in)– Scanner class allows the user to read values of various types. new creates a java object and allocates memory for it in heap. System.in is an input stream.

 scanner.nextInt() – Scans the next token of the input as an int

 

Output:

C:\Users\santo\Desktop\new>javac Addition.java

C:\Users\santo\Desktop\new>java Addition

*** www.programmingposts.com ***

<< JAVA Program to Add Two Integers >>

Enter the first number to be added:
8
Enter the second number to be added:
6
Addition of two numbers is 14

Screenshot of Output: