Translate

Wednesday 13 August 2014

Understanding Basic Java Program

class Demo
{
    public static void main(String args[])
    {
        int a=2, b=3, sum;
        sum= a+b;
        System.out.print("Total is:-" + sum);
    }
}

Execute it in your command prompt as discussed earlier.

Lets understand this program:-
Here class Demo creates a class by name Demo.

public specifies that the method is accessible from outside the class. (Will be discussed later in detail)

static specifies that the method is accessible without creating an object. (Will be discussed later in detail)

void specifies that the method main does not return any result to the calling environment.

String args is to store command line arguments.

int a=2, b=3, sum;   It creates 3 variable a, b, sum of integer type and assign values 2 and 3 to a and b

sum= a+b; It adds a and b and stores it in sum.

System.out.print("Total is:-" + sum); It prints Total is:- 5


Instead of System.out.print we can also use System.out.println which prints a new line character in the end.

No comments:

Post a Comment

Total Pageviews