Feb 7, 2012

[Java] basic code (class, main method, println)

class Hello
{
    public static void main(String args[ ])
    {
          System.out.println("Hello");  // Print Hello on screen
    }
}

1. class Hello
  1) All java programs start with "class" or "public class".  "public class" is unique in the package and can be used in all files of the package.
  2) "Hello" is class name.

2. public static void main(String args[ ])
  1) This defines main method
  2) "public" --> it applies for all files,  "static" --> first loaded in memory
  3) "void"  -->  no return value
  4) "main"  --> name of main method (it must be "main" if it is main method)
  5) "String"  --> type,  "args" --> name of array,  "[ ]" --> array

3. System.out.println("Hello");
  1) "System.out.println" --> a function of displaying something
        System = class name
        out = static variable
        println = method name   
  2) "Hello" --> something to be displayed on screen
  3) ";" --> symbol that indicates the end of command line

4. // Print Hello on screen
  1) //  -->  symbol that indicate annotation starts.  Once annotation starts in a line, all words or sentences behind "//" symbol are regarded annotation.


To sum up,

class class_name
{
    public static void main(String args[])       // declare class variables
    {
          // actual code for the program
    }
}

No comments:

Post a Comment