Taking a closer look towards program

We will have a closer look at program written in last post Compilation and execution: -
/*
This program welcomes you to the world of Java
Call this file Welcome.java
*/
class Welcome
{
//Your program starts with main
public static void main(String args[])
{
String str="Java";
System.out.println("Welcome to the world of " +str);
}
}
/*
This program welcomes you to the world of Java
Call this file Welcome.java
*/
This is a comment. This is a multiline comment. The compiler ignores the comments. The general rules of programming are not applicable to comments.It is enclosed within /* and */.
Though it is not necessary to write comments, it is a good practice to begin a program with comment indicating the purpose of program, its author and date on which program was written.
You can insert a comment anywhere in a program. Java supports three types of comments: -
Single-line comment
Multi-line comment
Documentation comment
Sometimes it is troublesome to find out what a particular statement accomplishes, that time it is worthwhile to include a comment. It is important when dealing with complex programs. A well-commented code is essential when a team is building big software.
class Welcome
{
The class is a keyword that is used to declare class Welcome. The [{](opening brace) marks the start of class Welcome and it ends with [}](closing brace).
Next line: -
//Your program starts with main
It is a single-line comment which starts with a //.
The next line of program include: -
public static void main(String args[])
This line declares main() method. As stated in previous comment the program starts executing with main()(This is similar to C/C++).
This line is used frequently in Java programs. Lets dig a little bit and understand what each word means: -
public is an access specifier. It controls the visibility of the class members. When public is defined every class members can be accessed outside the class in which it is declared.
The keyword static allows main() to be called without having to instantiate a particular instance of the class.
The keyword void states that the method does not return a value. It tells that the main() method does not return a value.
String args[] defines a parameter args[] which is an array of instances of class String. args[] receives any command-line arguments if any, during the execution of a program.
The next line of code is shown here: -
String str="Java";
String is a keyword that declares character type string. The above statement declares String variable str, which is assigned a value “Java” with assignment operator =.In Java Strings are stored using ASCII values. Keep in mind that the string is terminated using null character. The statement terminates with semicolon. Every statement in Java program terminates with a ; .
Next line: -
System.out.println("Welcome to the world of " +str);
This line outputs the string ‘Welcome to the world of’. In this statement, the plus sign appends the value of str to the string that precedes it. The program is terminated using closing braces.
Previous
Next Post »