CodersEditor Tutorials

CodersEditor Tutorials

  • Online IDE
  • Q&A
  • C Programming
  • Java
  • GraphQL.NET
  • System Software

›Java Examples

Java

    Introduction

    • Java - Overview
    • Installation & Setup
    • Program Structure
    • Keywords & Identifiers
    • Basic Data Types
    • Access Modifiers
    • Operators
    • Loops
    • Decision Statements

    Java Examples

    • Print an Integer
    • Add Two Integers
    • Multiply Two Floating Point Numbers
    • Find Ascii Value
    • Compute Quotient & Remainder
    • Swap Two Numbers using Temporary Variable
    • Swap Two Numbers without using Temporary Variable

Java Program to Add Two Integers

In this java program, let's learn how to add two integer numbers in Java and display the result in the console window.

How to Add Two Integers in Java?

Sourcecode

/*
    Author : CodersEditor.com
    Program : Java Program to Add Two Integers
*/
public class Main {

    public static void main(String[] args) {
        
        int firstNumber = 7;
        int secondNumber = 5;

        System.out.println("First Number :" + firstNumber );
        System.out.println("Second Number :" + secondNumber );
        
        int result = firstNumber + secondNumber;

        System.out.println("The sum is of two number is: " + result);
    }
}

Output

First Number :7
Second Number :5
The sum is of two number is: 12

Explanation

  • In this program, the two integer values 7 and 5 is stored in the variable firstNumber and secondNumber respectively.
  • The two numbers are displayed on the console using System.out.println.
  • The two numbers are added using the addition operator (+) and the result of the sum is stored in the variable called "result" and then displayed in the UI.
note
  • The + operator is unique in the sense that when you use it with the string, it concatenates two string and when used for integers, it adds them.
← Print an IntegerMultiply Two Floating Point Numbers →
  • How to Add Two Integers in Java?
    • Sourcecode
    • Output
    • Explanation
CodersEditor Tutorials
CoderdEditor Developer Tools
CodersEditor Online EditorGit Flavoured Markup EditorFree Online JSON FormatterFind My IP Address Details
Tutorials
C ProgrammingJava
Copyright © 2021 CodersEditor.com