Loading, please wait...

Addition of two variables in Java

Aug 17, 2019 Java, Code, 8777 Views
In this article, we will learn how to add two integers in java

Let's create the addition of two numbers program:

class add
{
     public static void main(String[] args)
     {
          int a=10;
          int b=20;
          System.out.println(a+b);
      }
}

Parameters used in the Program

  • class - class keyword is used to declare a class in java.
  • public - public keyword is an access modifier. It is the most non-restricted type modifier. It means it is visible to all.
  • static - static is a keyword. If we declare any method as static, it is called a static method. The advantage of the static method is that we don't have to create an object to access it. The main method is executed by the JVM, so it doesn't require to create an object to invoke the main method.
  • void - the void is a return type of a method. It means that it does not return any value.
  • main - It represents the starting point of the program.
  • String[] args - In java args contains the supplied command-line arguments as an array of string.
  • int - It is used to store the integer value.
  • System.out.println() - It is used to print the statement. The system is a class, out is the object of PrintStream class, println() is the method of PrintStream class which displays the result and then throws the cursor to the next line.

Difference between System.out.println() and System.out.print()

The only difference between println() and print() method is that println() throws the cursor to the next line after printing the desired result whereas print() method keeps the cursor on the same line. 

Example -

print() method.

System.out.print("Tutorial ");
System.out.print("Links");

The output for these statements will be-

Tutorial Links

println() method.

System.out.println("Tutorial ");
System.out.println("Links");

The output for these statements will be-

Tutorial
Links