Sample Java Program to Add Two Numbers (Beginner)
- Every line of code in Java must be inside a class. In the example below the class name is AddNumber. Class name should start with uppercase letter.
- A class may contain variables, methods and constructors. In the example below num1, num2 and sum are variables, main, calculateSum and printSum are methods.
- Constructors are special methods that have same name as the class name and are used to initialize the class valiables. In the example below AddNumber is a constructor. This constructor is used to assign value to variables num1 and num2.
- Methods are collection of statements that perform some specific task and may return the result to the caller. calculateSum method in the example below calculates sum by adding two numbers. printSum method prints the statement showing the sum.
- Object is an instance of a Java class. Objects have behavior of their class. In the example below obj is the object of class AddNumber. This object is then used to call the methods calculateSum and printSum.
- main method with signature public static void main(String[] args) is the entry point of the java program. This method is first called when the program is executed.
- System.out.println is used to print an argument passed to it on the console.
- Comments are used to explain Java code and to make it more readable. These are not executed by the compiler. In the example below the statements starting // are comments.
public class AddNumber {
// two integer variables num1 and num2
// and a variable "sum" to store the result
int num1, num2 , sum;
AddNumber(int num1, int num2) {
this.num1 = num1;
this.num2 = num2;
}
public static void main(String[] args) {
AddNumber obj = new AddNumber(5, 2);
obj.calculateSum();
obj.printSum();
}
private void calculateSum() {
//calculating the sum of num1 and num2 and
//storing the result in the variable sum
sum = num1 + num2;
}
private void printSum() {
//printing the sum
System.out.println("Sum of " + num1 + " and " + num2 + " is: " + sum);
}
}
Key Concepts Explained
✅ Classes
A class is the blueprint for creating objects. In the example, AddNumber
is the class.
✅ Constructors
The constructor AddNumber(int num1, int num2)
initializes the object's data members when it's created.
✅ Methods
-
calculateSum()
adds the two numbers. -
printSum()
displays the result on the console.
✅ Objects
obj
is an object of the AddNumber
class. It is used to call methods.
✅ main Method
This is the entry point of any Java application:
✅ Comments
Lines starting with //
are comments, used to make code easier to understand.
✅ Output
How to Run the Program
To run this program:
-
Open your IDE (e.g., IntelliJ, Eclipse, NetBeans)
-
Create a new Java project
-
Create a class file named
AddNumber
-
Paste the above code into the class file
-
Click the Run button or use the terminal to compile and run
This simple Java program not only adds two numbers but also teaches you how Java classes, constructors, methods, and objects work together in a real application. Once you're confident with this example, try modifying the code to take user input or handle different data types. Continue exploring more Java examples and concepts at itcodescanner.com to advance your programming skills step-by-step.
FAQ Section
Q1. Why do we use constructors in Java?
Constructors initialize objects when they are created, often setting default values or accepting parameters for initialization.
Q2. What is the purpose of the main method?
The main()
method is the entry point of every Java application. It is where program execution begins.
Q3. Can methods in Java return values?
Yes, methods can return values. In this example, the calculateSum()
method stores the result in a variable, but it could also return the sum.
Q4. What does System.out.println()
do?
It prints text or values to the console, followed by a new line.
Q5. Why is class name capitalized in Java?
By convention, class names in Java start with an uppercase letter for better readability and consistency.
Leave a Comment