I will write down the simple java program first and will explain each and every line of this program followed by it.
First Simple Java Program
/*
This is a simple Java Program.
Call this file 'Example.java'
*/
class Example
{
// Your program begins with a call to main() method.
public static void main(String args[])
{
System.out.println("This is a simple java program");
}
}
Output of the above program is: This is a simple java program
Step by Step explanation of the above program:
First Simple Java Program
/*
This is a simple Java Program.
Call this file 'Example.java'
*/
class Example
{
// Your program begins with a call to main() method.
public static void main(String args[])
{
System.out.println("This is a simple java program");
}
}
Output of the above program is: This is a simple java program
Step by Step explanation of the above program:
The program beings with the following lines. /* This is a simple Java Program. Call this file 'Example.java' */ This is a multiline comment which starts with /* and ends with */. All the lines between /* and */ will be treated as comments and wont be executed by JVM. This syntax is mainly used if there are more than one lines of comments. |
class Example { } We can clearly understand from the code that we have created a class named Example. In Java, all the code must reside inside a class by writing the code inside the braces i.e. { and } of the class. Note - The name of the class should match the name of the file that holds this program. In this example, the name of the class is Example , so while saving this program having the class Name as Example, we've to save the program file as Example.java. If we save the program file with a different name then your program wont execute. And also observe that if the class name is Example and you are going to save the program file as example.java, the program wont run as the class name and program file name wont match in case. |
// Your program begins with a call to main() method This is a single line comment that starts with // and ends at the end of the line. JVM wont execute the single line comments. |
public static void main(String args[]) { } This is the line at which the program will begin execution. All the Java applications will begin execution by calling main() method. There can be more than one method inside a class but the program execution starts from main() method only. The main method may further call the other methods in the class for execution. Also we've to understand the usage of public, static, void and String args[] terms in this simple program.
All the code related to the main() method should be written inside the braces { and } of the main( ) method. The same applies to the other normal methods.
|
System.out.println("This is a simple Java program."); This line of code is written inside the main( ) method in the above simple program. This line prints This is a simple Java program. output on execution. We've to understand the 'println( )', 'System' and 'out' terms to understand how this line prints the This is a simple Java program text as output.
Also you may notice that the above line ends with a semicolon ; . All the statements in Java end with a semicolon. All the other lines in the above simple program which don't end with the semicolon are not statements. |
No comments:
Post a Comment