Java Basic Syntax

  • 2021-12-11 17:43:00
  • OfStack

Directory 1, Basic Syntax 2. Identifier (legal variable name) 3, keyword (reserved word) 4, comment 5. Java package 5.1 package statement 5.2 import statement 6. Source file declaration rules

Foreword:

Java is an object-oriented interpretive programming language.

Object-oriented means that we should put 1 Java The program is a collection of 1 series of objects. Our job is to build these objects and make them work together by calling each other's methods to solve practical problems.

Explanatory means:

The Java compiler generates ByteCode ( byte-code ) instead of the usual machine code. The same code is designed to efficiently transfer programs to multiple platforms.

1. Basic grammar

Java case sensitive Java source file name must be the same as class name (compile error if file name and class name are not the same) The Java program consists of public static void main(String []args) Method begins execution.

2. Identifier (legal variable name)

Java identifiers can contain the following characters:

Letters: A-Z、a-z Numbers: 0-9 Symbol: $、_

The first character cannot be a number
Java Identifiers are case sensitive
Keyword cannot be used as an identifier

3. Keywords (reserved words)

关键字 含义
abstract 表明类或者成员方法具有抽象属性
assert 断言,用来进行程序调试
boolean 基本数据类型之1,声明布尔类型的关键字
break 提前跳出1个块
byte 基本数据类型之1,字节类型
case 用在switch语句之中,表示其中的1个分支
catch 用在异常处理中,用来捕捉异常
char 基本数据类型之1,字符类型
class 声明1个类
const 保留关键字,没有具体含义
continue 回到1个块的开始处
default 默认,例如,用在switch语句中,表明1个默认的分支
do 用在do-while循环结构中
double 基本数据类型之1,双精度浮点数类型
else 用在条件语句中,表明当条件不成立时的分支
enum 枚举
extends 表明1个类型是另1个类型的子类型,这里常见的类型有类和接口
final 用来说明最终属性,表明1个类不能派生出子类,或者成员方法不能被覆盖,或者成员域的值不能被改变,用来定义常量
finally 用于处理异常情况,用来声明1个基本肯定会被执行到的语句块
float 基本数据类型之1,单精度浮点数类型
for 1种循环结构的引导词
goto 保留关键字,没有具体含义
if 条件语句的引导词
implements 表明1个类实现了给定的接口
import 表明要访问指定的类或包
instanceof 用来测试1个对象是否是指定类型的实例对象
int 基本数据类型之1,整数类型
interface 接口
long 基本数据类型之1,长整数类型
native 用来声明1个方法是由与计算机相关的语言(如C/C++/FORTRAN语言)实现的
new 用来创建新实例对象
package
private 1种访问控制方式:私用模式
protected 1种访问控制方式:保护模式
public 1种访问控制方式:共用模式
return 从成员方法中返回数据
short 基本数据类型之1,短整数类型
static 表明具有静态属性
strictfp 用来声明FP_strict(单精度或双精度浮点数)表达式遵循IEEE 754算术规范[1] 
super 表明当前对象的父类型的引用或者父类型的构造方法
switch 分支语句结构的引导词
synchronized 表明1段代码需要同步执行
this 指向当前实例对象的引用
throw 抛出1个异常
throws 声明在当前定义的成员方法中所有需要抛出的异常
transient 声明不用序列化的成员域
try 尝试1个可能抛出异常的程序块
void 声明当前成员方法没有返回值
volatile 表明两个或者多个变量必须同步地发生变化
while 用在循环结构中

4. Notes

The annotations in Java are the same as those in C, including /* ... */ And // ... Two.

Example:


/* 
 Multiple lines 

 Notes 

*/

/*  Inline comment  */

//  End-of-line comment 

5. Java package

Packages are mainly used to classify classes and interfaces. When developing Java Program, you might write hundreds of classes, so it's necessary to classify classes and interfaces.

5.1 package Statement

byte-code0 The specified information directly generates the generated class Files are generated to the corresponding directory. Such as package aaa.bbb.ccc The compiler generates the classes under the. java file to the ./aaa/bbb/ccc/ This directory.

5.2 import Statement

import Is to simplify the use of byte-code0 After that, the instantiated code. Hypothesis ./aaa/bbb/ccc/ Under A Class:

If not import Instantiate the A class as: new aaa.bbb.ccc.A();

Use import aaa.bbb.ccc.A After, you can directly use the new A() It's over; That is, the compiler matches and extends the aaa.bbb.ccc. This string.

For example, the following command line will command the compiler to load all classes under the java_installation/java/io path:

import java.io.*;

6. Source file declaration rules

You can only have 1 source file in 1 source file public Class (non-public unlimited) The name of the source file should be the same as that of the public The class name of the class remains 1. (If the class name of the public class in the source file is Employee, the source file should be named Employee. java) If 1 class is defined in a package, then byte-code0 Statement should be on the first line of the source file. If the source file contains import Statement, you should put it in the byte-code0 Statement and class definition. If not package Statement, then import Statement should be at the front of the source file. import Statement and byte-code0 Statement is valid for all classes defined in the source file. You cannot declare different packages for different classes in the same 1 source file.

Related articles: