一.关键字
学习一件东西,需要习惯从它官方文档里面学习。毕竟羊毛出在羊身上嘛。下面贴了一张官网图,看不清可以点击下方链接。
直达官网关键字链接
下面是对每个关键字的具体描述,我们需要习惯用英文去理解。所以没有特意翻译。
Keyword | Usage |
---|---|
abstract | Specifies that a class or method will be implemented later, in a subclass |
assert | Assert describes a predicate placed in a Java program to indicate that the developer thinks that the predicate is always true at that place. |
boolean | A data type that can hold True and False values only |
break | A control statement for breaking out of loops. |
byte | A data type that can hold 8-bit data values |
case | Used in switch statements to mark blocks of text |
catch | Catches exceptions generated by try statements |
char | A data type that can hold unsigned 16-bit Unicode characters |
class | Declares a new class |
continue | Sends control back outside a loop |
default | Specifies the default block of code in a switch statement |
do | Starts a do-while loop |
double | A data type that can hold 64-bit floating-point numbers |
else | Indicates alternative branches in an if statement |
enum | A Java keyword is used to declare an enumerated type. Enumerations extend the base class. |
extends | Indicates that a class is derived from another class or interface |
final | Indicates that a variable holds a constant value or that a method will not be overridden |
finally | Indicates a block of code in a try-catch structure that will always be executed |
float | A data type that holds a 32-bit floating-point number |
for | Used to start a for loop |
if | Tests a true/false expression and branches accordingly |
implements | Specifies that a class implements an interface |
import | References other classes |
instanceof | Indicates whether an object is an instance of a specific class or implements an interface |
int | A data type that can hold a 32-bit signed integer |
interface | Declares an interface |
long | A data type that holds a 64-bit integer |
native | Specifies that a method is implemented with native (platform-specific) code |
new | Creates new objects |
null | This indicates that a reference does not refer to anything |
package | Declares a Java package |
private | An access specifier indicating that a method or variable may be accessed only in the class it’s declared in |
protected | An access specifier indicating that a method or variable may only be accessed in the class it’s declared in (or a subclass of the class it’s declared in or other classes in the same package) |
public | An access specifier used for classes, interfaces, methods, and variables indicating that an item is accessible throughout the application (or where the class that defines it is accessible) |
return | Sends control and possibly a return value back from a called method |
short | A data type that can hold a 16-bit integer |
static | Indicates that a variable or method is a class method (rather than being limited to one particular object) |
strictfp | A Java keyword is used to restrict the precision and rounding of floating-point calculations to ensure portability. |
super | Refers to a class’s base class (used in a method or class constructor) |
switch | A statement that executes code based on a test value |
synchronized | Specifies critical sections or methods in multithreaded code |
this | Refers to the current object in a method or constructor |
throw | Creates an exception |
throws | Indicates what exceptions may be thrown by a method |
transient | Specifies that a variable is not part of an object’s persistent state |
try | Starts a block of code that will be tested for exceptions |
void | Specifies that a method does not have a return value |
volatile | This indicates that a variable may change asynchronously |
while | Starts a while loop |
sealed | The sealed keyword is used to declare a class as “sealed,” meaning it restricts which classes can extend it. |
permits | The permits keyword is used within a sealed class declaration to specify the subclasses that are permitted to extend it. |
二.标识符
在 Java 中,标识符用于识别目的。Java 标识符可以是类名、方法名、变量名或标签。
public class Test
{
public static void main(String[] args)
{
int a = 20;
}
}
- Test: 类名
- main: 方法名
- String: 预定义类名
- args: 变量名
- a: 变量名
标识符注意点:
1.所有的标识符只能以大小写字母(A-Z或a-z),美元符号($),或者下划线(_)开头,首字符之后可以任意组合。但是不能以#,%等特殊字符结尾。(&可以)
2. 不能使用关键字作为变量名或方法名,也就是第一大点讲到的图里的关键字。
3.标识符区分大小写,也就是大写字母的变量和小写字母的变量是完全不一样的。十分之敏感。
4.养成习惯用英文方式命名。name就name,不要用mingzi代替,非常不规范。
这里顺便说一个快捷键:command + D 复制光标所在行内容。省事不少。(windows是ctrl+D)
三.关键字和标识符区别
较为潦草地说除了关键字图里的那些字母,其他符合规范的命名字符均为标识符。
最后,想啰嗦几句。我们在写代码之初,了解代码规范,养成良好的习惯是很重要。所有的理论需要投入实践,更准确的说是需要投入大量的实践。在实践中思考,在实践中总结,相信我们都会在混沌中找到一方属于自己的净土。