Well,now the post that you all have been waiting for.Today I'll start programming with you guys.
First let me write the program and then I will explain each component of it.
Program 1:Hello World
class Demo
{
public static void main(String args[])
{
System.out.println("Hello World");
}
}
{
public static void main(String args[])
{
System.out.println("Hello World");
}
}
Now let me explain you guys the meaning of the code.
1. In Java each and every program must be written within a class.Unlike C++,where we can write programs outside class Java does not allow you to write your code outside the class.Hence by writing "class Demo" I have created a class by the name of Demo.You can write any name provided you follow a few rules(I will explain it as we move ahead to complex programs).For now lets name the class "Demo".
1. In Java each and every program must be written within a class.Unlike C++,where we can write programs outside class Java does not allow you to write your code outside the class.Hence by writing "class Demo" I have created a class by the name of Demo.You can write any name provided you follow a few rules(I will explain it as we move ahead to complex programs).For now lets name the class "Demo".
2. "public static void main(String args[])":
public:It is a type of "access specifier".It defines the type of access to the program.There are different types of access specifiers available.But for now all our programs shall have public type access specifier.We shall discuss them in greater detail later.
static:It identifies the method as a class method.It means that we do not need to call these functions via a object.We can directly call them.We shall always keep the main method as static.You shall understand the function of static better when I shall write a few programs.
void:Each and every function has to return a value.If the function does not require to return any value then the return type of the function is void.We will always keep the return type of the main method as void.Once we start methods we shall start returning values.But till then lets use void.
main:"main" is a keyword in java.When we run the executable file it is from the main that execution starts.Even the execution of the most advanced code starts from the main and only the main.
String args[]:It is compulsory to write "String args[]" in the main method.It is used for receiving any arbitrary
number of arguments and save it in the array.
3. System:It is a class that we are calling for displaying our output.
4. out:It is an object of the class "System" via which output can be displayed.
5. println:It is a method of "System" class which enables us to display what we want to.
NOTE:
In java all opening curly braces ie "{" must have a corresponding closing brace ie "}".
Java is a case sensitive language.So please do not change the syntax of any word.
All the programs that I will be posting shall be compiled and shall work perfectly.So you need not worry that the logic and the errors.
No comments:
Post a Comment