Friday, January 25, 2013

What Hello program is made of


The first seven lines of the Hello program are comments. Comments are non-executable statements and you can put them anywhere---inside or outside a class. You can write comments in three ways.
  1. // This is an in-line comment, effective only on the current line. Anything to the right of this comment is ignored. This is suitable for commenting individual lines of code
  2. /* */ This is a block comment, anything in between the inner asterisks will be ignored by the compiler. These can span multiple lines
  3. /** */ Another block comment that is used by JavaDoc, don't worry about this right now. Just experiment with these three for the time being
After the comments is a class block. Java is an OOP language, and a strict one at that. You cannot write anything meaningul outside a class or interface. Even a simple output to the screen has to be within the context of a class. This is the reason why you need to be very comfortable with the structure of the Hello program as early as now---you will not go very far otherwise.
A Java class is constructed using the keyword class followed a class name. The class name is something that you will define, in our example, the name of the class is Hello. The class name is followed by a pair of curly braces. Anything inside the curly braces constitutes the body of the class.
You might have noticed that our example class name is Hello and that is stored in a file namedHello.java. That is incidental and not a requirement at all. It would have been a requirement if we made the class public. If we changed our construction to declare a public class
public class Hello {
    public static void main(String args[]) {

    }
}
then it becomes mandatory that the name of class be consistent with the name of the source file. This is one of the many rules of Java.
The name of the generated object file was Hello.class not because the name of source file isHello.java but because the name of the class is Hello. The name of class affects the name of the object file.
Inside the class is a main() function,  this is a runtime requirement. Any class that you will pass on the JRE requires a main function. Not all classes will require this function, only those classes that you will use as a starting point of your program. In our example, class Hello is a program starting point.
Java has a very specific format for a main function. The function needs to be publicstatic & void. Public static and void are keywords. Each have a specific action but we will defer their discussion to later chapters.
Inside the main function is System.out.println(). This is a very common command in Java. You can practically put anything inside the parentheses and it will be printed to the screen. The string literal "Hello World" was placed inside the function println(). String literals are defined be enclosing words using a pair of double quotation marks. You cannot define a string literal using single quotes---that results into a different action.
System.out.println("Hello World"); is a simple statement. It invoked the println method of System.out. It is possible to create more complex statements in Java. You need to remember to put a semi-colon after each statement---simple or complex.


Call me and you will get the knowledge and skills you need instantly.
For inquiries contact me on my mobile number or email me at inquiry@eglobiotraining.com. View my multiply site http://erwinglobio.multiply.com
 
 
Prof. Erwin M. Globio, MSIT
Senior IT Lecturer
Far Eastern University
Email Address: inquiry@eglobiotraining.com
Skype: erwinglobio
 
 
Call Now:
 
SMART: 09393741359
SUN: 09323956678



No comments:

Post a Comment