Appearance
Basic structure
Let's first analyze a complete Java program and what is its basic structure:
java
/**
* Comments that can be used to automatically generate documentation
*/
public class Hello {
public static void main(String[] args) {
// Output text to the screen:
System.out.println("Hello, world!");
/* Multi-line comment start
Comment content
Comment end */
}
} // Class definition end
Because Java is an object-oriented language, the basic unit of a program is class
. class
is a keyword. The class
name defined here is Hello
:
java
public class Hello {
// ...
}
Class Name
Class name requirements:
- Class names must begin with an English letter, followed by a combination of letters, numbers, and underscores.
- Begin with a capital letter
Pay attention to abiding by naming conventions, good class naming:
- Hello
- NoteBook
- VRPlayer
Bad class naming:
- hello
- Good123
- Note_Book
- _World
Note that public is the access modifier, indicating that the class is public.
Even if you don't write public , it will compile correctly, but this class will not be executed from the command line.
Within class
, several methods can be defined:
java
public class Hello {
public static void main(String[] args) { // The method name is 'main'
// some code
}
}
Method Name
A method defines a set of execution statements, and the code inside the method will be executed sequentially.
The method name here is main
, and the return value is void
, which means there is no return value.
We noticed that in addition to modifying class
, public
can also modify methods. The keyword static
is another modifier, which represents a static method.
We will explain the type of method later. At present, we only need to know that the method specified by the Java entry program must be a static method, and the method name must be main
. The parameter must be a String array.
Method names also have naming rules. The naming is the same as class
, but the first letter is lowercase:
Good method naming:
- main
- goodMorning
- playVR
Bad method naming:
- Main
- good123
- good_morning
- _playVR
Inside the method, the statements are the actual execution code. Every line of statement in Java must end with a semicolon:
java
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!"); //statement
}
}
Comments
In a Java program, a comment is a text that is read by humans and is not part of the program, so the compiler will automatically ignore the comment.
There are three types of comments in Java. The first is a single-line comment, starting with a double slash and ending at the end of the line:
java
// comments...
Multi-line comments start with /*
asterisk and end with */
, and can have multiple lines:
java
/*
multi-line
comments
*/
There is also a special multi-line comment that starts with /**
and ends with */
. If there are multiple lines, each line usually starts with an asterisk:
java
/**
*
* @auther your name
*/
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
This special multi-line comment needs to be written at the definition of classes and methods and can be used to automatically create documentation.
Java programs do not have clear requirements for format. A few more spaces or carriage returns will not affect the correctness of the program, but we must develop good programming habits and pay attention to abide by the coding format agreed by the Java community.
What are the requirements for the agreed encoding format?
In fact, the Eclipse IDE we introduced earlier provides the shortcut key Ctrl+Shift+F
(macOS is ⌘+⇧+F
) to help us quickly format the code.