Java basic features

Java is an object-oriented language, from such a simple program, you can see some of the basic features of a Java program :

  • Primary code in Java are located in a class by class classkeyword to define, as illustrated, the main code in HelloWorldclass:
    public class HelloWorld {
      ...
    }
  • Java is a case-sensitive language , such as:
    • classCan not be writtenClass
    • Named HelloWorldclass name and Helloworldthe class is not the same
  • The file name of the program must be identical and the name of the class file Java code to class names are .javanamed suffix
    • For example, HelloWorldthe class code is stored in a file HelloWorld.java
  • public static void main(String[] args)It is a method, which is the entrance of the Java program
    • Any code Java program is executed from the beginning of this method
  • System.out.println("Hello");Is to call a method, this line of code output to the consoleHello
    • You will want to put output information System.out.println(...)can be displayed in the console in brackets
    • Attention System.out.print()and System.out.println()distinction, which will last a newline increase
  • Keyword

        The above code public, classand staticso on are all words Java language keywords.

        Keywords also known as reserved words, the Java language to retain these words for a particular purpose, they constitute the basic elements of the Java language syntax.

        For example, classkeyword indicates a class, Java compiler sees the word to know that this is the definition of a class.

  • Identifier

       Various components of the Java program name are required. Class name, variable name, method name, parameters name and so is referred to as an identifier. Such as HelloWorldthe class name in the code is a kind of identifier. We own identifier is named.

       About Java identifier, we need to know the following:

  • All identifiers should be a letter (AZ or az), dollar sign ( $), or an underscore ( _start)
  • After the first character may be a letter (AZ or az), dollar sign ( $), underscore ( _combination) and digital
  • Java language comes with keywords can not be used as an identifier, for example, you can not define a class or method namedclass
  • Identifiers are case sensitive

       For example blog, $user, _titleand __1_contentare valid identifier; and 123blogand -userare illegal identifier.

       For readability, in general, the class names start with a capital letter, such HelloWorlda capital letter H at the beginning; General methods start with a lowercase letter, such mainmethods start with a lowercase letter m. If the name contains several words, starting from the second word of each word capitalized, named this way is called hump nomenclature.

  • Code comments

       Add comments to explain the program can be used to program the function and role of certain parts, to improve readability. Comments do not affect the functionality of the program.

       Comments can also be temporarily masked some code debugging, uncomment complete debugging and allows the code to re-play a role.

       Java comments in divided into three types.

  • Single-line comments: annotation content before adding two slashes //, the Java compiler will ignore //information
  • Multi-line comments: added in front of the content to be annotated /*, added after the content of the comment*/
  • Documentation Note: in front of the comment content to add /**, add in the content of the comment */, which is a special multi-line comments, the contents of the notes can be used to document generation program, after we explain the specific use.

       In HelloWorldthe code, for example, increasing the three types of annotations, as follows:

/ ** 
 This is a documentation comment 
* /  
public  class the HelloWorld {   
    
    / * 
     This is a multi-line comment. 
     In the main printing process hello message 
    * / 
    
    public  static  void main (String [] args) {
         // print information, which is a single line comment 
        System.out.println ( "the Hello World"); // this is a single-line comment, You may be behind a program statement 
    } 
    
}

Guess you like

Origin www.cnblogs.com/mliu/p/11297790.html