Java basic grammar
★ Foundation Stage
-
We usually write code in less time than the code, we can also read their own writing, but when once the project structure complicated, we need to use the Notes.
-
Notes and will not be executed, is to give our people to write code to read.
-
Write notes is a very good habit
▲ Java annotations in three ways:
-
Single-line comments
-
Multi-line comments
-
Documentation Comments
public class the Test { public static void main ( String [] args) { // comment line of text can be a single line comment // output a Hello, World! . System . OUT println ( "Hello, World!"); // multi-line comments can annotate a text / * * / / * output a Hello, World! Output a Hello, World! Output a Hello, World! Output a Hello, World! Output a Hello, World! * / // JavaDoc documentation comments / ** * / / ** * @Description the HelloWorld * @author know fearless * * / } }
Keyword
turn off | key | word | ||
---|---|---|---|---|
abstract | abstract | abstract | break | byte |
case | catch | char | class | const |
continue | default | do | double | else |
enum | extends | final | finally | float |
for | goto | if | implements | import |
instanceof | int | interface | long | native |
new | package | private | protected | public |
return | strictfp | short | static | super |
switch | synchronized | this | throw | throws |
transient | try | void | volatile | while |
Keyword | meaning |
---|---|
abstract | Members of the class or method show abstract properties |
assert | Assertions, used for debugging |
boolean | One of the basic data types, declare Boolean types of keywords |
break | Advance out of a block |
byte | One of the basic data types, Type Byte |
case | Represents a branch which is used in the switch statement, |
catch | In exception processing for capturing an abnormal |
char | One of the basic data types, character types |
class | Declare a class |
const | Reserved keywords, there is no specific meaning |
continue | Back to the beginning of a block |
default | By default, for example, used in a switch statement, indicating that a default branch. In Java8 also act on the declaration of default implementation of the interface functions |
do | Used in the do-while loop structure |
double | One of the basic data types, double precision floating point type |
else | Used in a conditional statement, indicating that the time when the branch condition is not satisfied |
enum | enumerate |
extends | It indicates that a type is a subtype of another type. For a class, or it may be another class abstract class; For an interface, the interface may be another |
final | Used to describe the final attribute that identifies a class can not derive subclasses or members of the method can not be covered, or the value of the member of the domain can not be changed, the constants used to define |
finally | Statement block for processing exceptions, to declare a substantially certain to be performed to |
float | One of the basic data types, single precision floating point type |
for | An endless guide word structure |
goto | Reserved keywords, there is no specific meaning |
if | Guide word conditional statement |
implements | Showed that a class implements the given interface |
import | To access the show specified class or package |
instanceof | It used to test whether an object instance of the object type is designated |
int | One of the basic data types, integer type |
interface | interface |
long | One of the basic data types, long integer type |
native | Is used to declare a method implemented by the language associated with the computer (e.g., C / C ++ / FORTRAN language) |
new | To create a new instance of an object |
package | package |
private | An access control mode: the private mode |
protected | An access control method: Protected Mode |
public | 一种访问控制方式:共用模式 |
return | 从成员方法中返回数据 |
short | 基本数据类型之一,短整数类型 |
static | 表明具有静态属性 |
strictfp | 用来声明FP_strict(单精度或双精度浮点数)表达式遵循IEEE 754算术规范 |
super | 表明当前对象的父类型的引用或者父类型的构造方法 |
switch | 分支语句结构的引导词 |
synchronized | 表明一段代码需要同步执行 |
this | 指向当前实例对象的引用 |
throw | 抛出一个异常 |
throws | 声明在当前定义的成员方法中所有需要抛出的异常 |
transient | 声明不用序列化的成员域 |
try | 尝试一个可能抛出异常的程序块 |
void | 声明当前成员方法没有返回值 |
volatile | 表明两个或者多个变量必须同步地发生变化 |
while | 用在循环结构中 |
▲关键字一律用小写字母标识,按其用途划分为如下几组。
-
用于数据类型。 用于数据类型的关键字有 boolean、byte、char、 double、 false、float、int、long、new、short、true、void、instanceof。
-
用于语句。 用于语句的关键字有break、case、 catch、 continue、 default 、do、 else、 for、 if、return、switch、try、 while、 finally、 throw、this、 super。
-
用于修饰 用于修饰的关键字有 abstract、final、native、private、 protected、public、static、synchronized、transient、 volatile。
-
用于方法、类、接口、包和异常。 用于方法、类、接口、包和异常的关键字有 class、 extends、 implements、interface、 package、import、throws。
-
还有些关键字如cat、 future、 generic、innerr、 operator、 outer、rest、var等都是Java保留的没有意义的关键字。
-
Java还有3个保留字:true、false、null。它们不是关键字,而是文字。包含Java定义的值。和关键字一样,它们也不可以作为标识符使用。
标识符
▲Java 所有的组成都需要名字。类名、变量名以及方法名都被称为标识符。
※ 标识符注意点
-
所有的标识符都应该以字母(A-Z 或者 a-z),美元符($)或者下划线(_)开始
-
首字符之后可以是字母(A-Z 或者 a-z),美元符($)、下划线(_)或数字的任何字符组合
-
不能使用关键字作为变量名或方法名
-
标识符是大小写敏感的
-
合法标识符举例:name、$easy、_easy、__1_easy
-
非法标识符举例:123abc、-easy、#abc
public class Test {
public static void main(String[] args) {
//大小写十分敏感
String Name = "zhizhewuwei";
String name = "zhizhewuwei";
System.out.println(Name);
String Ahello = "zhizhewuwei";
String hello = "zhizhewuwei";
String $hello = "zhizhewuwei";
String _hello = "zhizhewuwei";
String _1 = "zhizhewuwei";
//String _# = "zhizhewuwei";
//String class = "zhizhewuwei";
//String 1hello = "zhizhewuwei";
//String #hello = "zhizhewuwei";
//String *hello = "zhizhewuwei";
}
}
※可以使用中文命名,但是一般不建议这样去用,也不建议使用拼音。