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
class
keyword to define, as illustrated, the main code inHelloWorld
class:public class HelloWorld { ... }
- Java is a case-sensitive language , such as:
class
Can not be writtenClass
- Named
HelloWorld
class name andHelloworld
the 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
.java
named suffix- For example,
HelloWorld
the class code is stored in a file HelloWorld.java
- For example,
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()
andSystem.out.println()
distinction, which will last a newline increase
- Keyword
The above code public
, class
and static
so 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, class
keyword 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 HelloWorld
the 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 named
class
- Identifiers are case sensitive
For example blog
, $user
, _title
and __1_content
are valid identifier; and 123blog
and -user
are illegal identifier.
For readability, in general, the class names start with a capital letter, such HelloWorld
a capital letter H at the beginning; General methods start with a lowercase letter, such main
methods 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 HelloWorld
the 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 } }