Table of contents
The difference between Java source program and compiled execution
Basic template (Hello World program)
public class HelloWorld {
/* 第一个Java程序
* 它将输出字符串 Hello World
*/
public static void main(String[] args) {
System.out.println("Hello World"); // 输出 Hello World
}
}
basic grammar
- Case-sensitive: Java is case-sensitive, which means that the identifiers Hello and hello are different.
- Class name: For all classes, the first letter of the class name should be capitalized. If the class name consists of several words, the first letter of each word should be capitalized, for example MyFirstJavaClass .
- Method name: All method names should start with a lowercase letter. If the method name contains several words, the first letter of each subsequent word is capitalized.
- Source file name: The source file name must be the same as the class name. When saving the file, you should use the class name as the file name (remember Java is case-sensitive), and the file name suffix is .java . (If the file name and class name are different, a compilation error will occur).
- Main method entrance: All Java programs are composed of public static void main(String[] args) The method starts executing.
Java identifier
All components of Java require names. Class names, variable names, and method names are all called identifiers.
There are a few things to note about Java identifiers:
- All identifiers should start with a letter (A-Z or a-z), dollar sign ($), or underscore (_)
- The first character can be followed by any combination of letters (A-Z or a-z), dollar sign ($), underscore (_) or numbers.
- Keywords cannot be used as identifiers
- Identifiers are case sensitive
- Examples of legal identifiers: age, $salary, _value, __1_value
- Examples of illegal identifiers: 123abc, -salary
Java modifiers
Like other languages, Java can use modifiers to modify methods and properties in classes. There are two main types of modifiers:
- Access control modifiers: default, public, protected, private
- Non-access control modifiers: final, abstract, static, synchronized
We will discuss Java modifiers in depth in later chapters.
Java keywords
Java keywords are listed below. These reserved words cannot be used in the names of constants, variables, and any identifiers.
category | Keywords | illustrate |
---|---|---|
Access control | private | private |
protected | be protected | |
public | public | |
default | default | |
Class, method and variable modifiers | abstract | Declare abstraction |
class | kind | |
extends | extend,inherit | |
final | final value, immutable | |
implements | Implementation (interface) | |
interface | interface | |
native | Native, native methods (non-Java implementation) | |
new | new, create | |
static | static | |
strictfp | Strict and precise | |
synchronized | thread, synchronization | |
transient | short | |
volatile | Volatile | |
program control statements | break | Break out of loop |
case | Define a value for switch to select | |
continue | continue | |
default | default | |
do | run | |
else | otherwise | |
for | cycle | |
if | if | |
instanceof | Example | |
return | return | |
switch | Select execution based on value | |
while | cycle | |
Error handling | assert | Assert whether an expression is true |
catch | catch exception | |
finally | Execute regardless of exception | |
throw | Throws an exception object | |
throws | declares that an exception may be thrown | |
try | catch exception | |
Package related | import | introduce |
package | Bag | |
basic type | boolean | boolean |
byte | Byte type | |
char | Character type | |
double | double precision floating point | |
float | single precision floating point | |
int | integer | |
long | long integer | |
short | Short | |
variable reference | super | parent class, super class |
this | This category | |
void | No return value | |
reserved keywords | goto | is a keyword, but cannot be used |
const | is a keyword, but cannot be used |
Note:Java's null is not a keyword, similar to true and false. It is a literal constant and is not allowed to be used as an identifier.
Comment
//行注释
/*
块注释
可以多行一起注释
*/
Java empty line
Blank lines or lines with comments will be ignored by the Java compiler.
inherit
In Java, a class can be derived from other classes. If you are creating a class and there is already a class that has the properties or methods you need, then you can inherit the newly created class from that class.
Using inheritance, you can reuse methods and properties of existing classes without having to rewrite the code. The inherited class is called a super class, and the derived class is called a subclass.
interface
In Java, an interface can be understood as a protocol for objects to communicate with each other. Interfaces play a very important role in inheritance.
The interface only defines the methods to be used by the derived class, but the specific implementation of the method completely depends on the derived class.
The difference between Java source program and compiled execution
As shown below: