The basic structure of a Java program

/ ** 
 * can be used to automatically create a document annotation 
 * / 
public  class the Hello {
     public  static  void main (String [] args) {
         // output to the screen text: 
        System.out.println ( "the Hello, world!" );
         / * multi-line comments start 
        footnotes 
        end of comment * / 
    } 
} // end of class definitions

Because Java is an object-oriented language, is the basic unit of a program class, classkeyword, here defined classname is Hello:

public  class the Hello { // class name is the Hello
     // ... 
} // end of class definitions

Class Requirements:

  • Class names must start with an English letter, followed by a combination of letters, numbers and underscores
  • Used to begin with a capital letter

Make sure you follow the naming convention, like a good name:

  • Hello
  • NoteBook
  • VRPlayer

Bad class named:

  • hello
  • Good123
  • Note_Book
  • _World

Notice publicis access modifier to indicate that classis public.

Do not write public, can compile correctly, but this class will not be executed from the command line.

In classthe interior, it is possible to define several methods (method):

public  class the Hello {
     public  static  void main (String [] args) { // method name main
         // method code ... 
    } // end method definition 
}

The method defines a set of statements executed, the code inside the methods will be sequentially executed sequentially.

Here the method name main, the return value voidindicates no return value.

We note that publicin addition can be modified class, but also can be modified method. The keyword staticis another modifier that represents the static method, we will explain later type of method, now we just need to know the method specified in Java entry program must be a static method, the method name must be mainthe parameters in brackets must a String array.

The method also has naming names, name and classthe same, but the first letter lowercase:

Good method name:

  • main
  • goodMorning
  • playVR

Methods bad name:

  • Main
  • good123
  • good_morning
  • _playVR

Within a method, the statement is the real code execution. Java each line statement must end with a semicolon:

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, world!"); // 语句
    }
}

In Java programs, comments gives a reading of the text, not part of the program, the compiler will automatically ignore the comment.

Java has three kinds of notes, the first one is the single-line comments, beginning with a double slash, until the end of the line ends:

// This is a comment ...

The multi-line comments /*begin with an asterisk to */the end, you can have multiple lines:

/ * 
This is a comment 
blablabla ... 
this is a comment 
* /

There is also a special multi-line comments, to /**the beginning to */the end, if multiple rows, each row usually begin with an asterisk:

/ ** 
 * Notes can be used to automatically create a document 
 * 
 * @auther liaoxuefeng 
 * / 
public  class the Hello {
     public  static  void main (String [] args) { 
        System.out.println ( "the Hello, World!" ); 
    } 
}

This particular multi-line comments need to write in the definition of the classes and methods that can be used to automatically create documents.

ava no program format specific requirements, a few more spaces or carriage returns do not affect the correctness of the program, but we have to develop good programming habits, attention to compliance with the Java community agreed encoding format.

That agreement encoding formats which ask for it? In fact, we described earlier in the Eclipse shortcuts Ctrl+Fhelp us quickly formatting function code, Eclipse is in accordance with the agreed encoding format code format, so only need to look at long-sawed after the code is formatted on the line. Specific requirements may Eclipse code format is provided in Java- Code Styleview.

 Liao teacher correspondence section links

 

Guess you like

Origin www.cnblogs.com/fqh123/p/10962340.html
Recommended