Thursday, June 06, 2013

Basics of Java Programming: How to write a simple Java Program

Leave a Comment Follow us on twitter
To do java programming and to become a better programmer, one should able to write the java code with less complexity and lower running time. In this java tutorial instead of focusing over Complexity and Running time of a program, we are going to learn how to write a simple java program.

class Javaprogram
{
public static void main(String args[])
{
System.out.println("This is your first java program");
System.out.print("Here you are going to learn java");
}
}

Above written eight lines of code is the simple java program one can write, but for the beginners it is very difficult to understand such creepy code. Let's understand the code. As Java is an Object Oriented Programming language so there is the use of classes, procedures, methods etc. Same applies here; in java program we start with class <Classname>. After that in the next line we use public static void main(String args[]).  Now the question arises what does that mean?

Why we use public static void main(String args[]) in java program


The answer is that public is used to make the code accessible by any object of any class from anywhere. The class is called by the object of that class, static is used to specify that it can be called without having any object, void is return type which means that nothing is returning as void means nothing/null, main is the main() function of the program. It is compulsory to use main() so that it should always called first by default. The use of String args[] is to take the command line arguments from user. The use of args[] is to specify the number of arguments to be stored in array and the return type of that array is String.

Then we use the System.out.println and System.out.print, here out means output which is belongs to System class; and println() and print() are the methods of that System class. The difference between println and print is that println show the output in next line while print shows the output in the same line.

I hope that now you will understand the above written java code and able to write java program of your own.

0 comments:

Post a Comment