1
0
mirror of https://github.com/Snailclimb/JavaGuide synced 2025-06-16 18:10:13 +08:00

[docs fix]java语言的关键字

`true`, `false`, 和`null`看起来像关键字但不是关键字。
This commit is contained in:
guide 2022-02-25 19:15:49 +08:00
parent 618eb20f8d
commit 1c1501a5b9
18 changed files with 101 additions and 93 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.7 KiB

After

Width:  |  Height:  |  Size: 4.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.6 KiB

After

Width:  |  Height:  |  Size: 4.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.3 KiB

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.0 KiB

After

Width:  |  Height:  |  Size: 5.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 33 KiB

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.3 KiB

After

Width:  |  Height:  |  Size: 5.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.6 KiB

After

Width:  |  Height:  |  Size: 9.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 163 KiB

After

Width:  |  Height:  |  Size: 160 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.9 KiB

After

Width:  |  Height:  |  Size: 5.4 KiB

View File

@ -121,90 +121,13 @@ JRE 是 Java 运行时环境。它是运行已编译 Java 程序所需的所有
### 字符型常量和字符串常量的区别?
1. **形式** : 字符常量是单引号引起的一个字符,字符串常量是双引号引起的 0 个或若干个字符
2. **含义** : 字符常量相当于一个整型值( ASCII 值),可以参加表达式运算; 字符串常量代表一个地址值(该字符串在内存中存放位置)
3. **占内存大小** 字符常量只占 2 个字节; 字符串常量占若干个字节 (**注意: `char` 在 Java 中占两个字节**)
1. **形式** : 字符常量是单引号引起的一个字符,字符串常量是双引号引起的 0 个或若干个字符
2. **含义** : 字符常量相当于一个整型值( ASCII 值),可以参加表达式运算; 字符串常量代表一个地址值(该字符串在内存中存放位置)
3. **占内存大小** 字符常量只占 2 个字节; 字符串常量占若干个字节
### 使用过可变长参数吗?
(**注意: `char` 在 Java 中占两个字节**)
从 Java5 开始Java 支持定义可变长参数,所谓可变长参数就是允许在调用方法时传入不定长度的参数。就比如下面的这个 `printVariable` 方法就可以接受 0 个或者多个参数。
```java
public static void method1(String... args) {
//......
}
```
另外,可变参数只能作为函数的最后一个参数,但其前面可以有也可以没有任何其他参数。
```java
public static void method2(String arg1, String... args) {
//......
}
```
**遇到方法重载的情况怎么办呢?会优先匹配固定参数还是可变参数的方法呢?**
答案是会优先匹配固定参数的方法,因为固定参数的方法匹配度更高。
我们通过下面这个例子来证明一下。
```java
/**
* 微信搜 JavaGuide 回复"面试突击"即可免费领取个人原创的 Java 面试手册
*
* @author Guide哥
* @date 2021/12/13 16:52
**/
public class VariableLengthArgument {
public static void printVariable(String... args) {
for (String s : args) {
System.out.println(s);
}
}
public static void printVariable(String arg1, String arg2) {
System.out.println(arg1 + arg2);
}
public static void main(String[] args) {
printVariable("a", "b");
printVariable("a", "b", "c", "d");
}
}
```
输出:
```
ab
a
b
c
d
```
另外Java 的可变参数编译后实际会被转换成一个数组,我们看编译后生成的 `class`文件就可以看出来了。
```java
public class VariableLengthArgument {
public static void printVariable(String... args) {
String[] var1 = args;
int var2 = args.length;
for(int var3 = 0; var3 < var2; ++var3) {
String s = var1[var3];
System.out.println(s);
}
}
// ......
}
```
### 注释有哪几种?注释越多越好吗?
### 注释有哪几种形式?
Java 中的注释有三种:
@ -239,23 +162,25 @@ Java 中的注释有三种:
### 标识符和关键字的区别是什么?
在我们编写程序的时候,需要大量地为程序、类、变量、方法等取名字,于是就有了标识符简单来说标识符就是一个名字。但是有一些标识符Java 语言已经赋予了其特殊的含义,只能用于特定的地方,这些特殊的标识符就是关键字。因此,关键字是被赋予特殊含义的标识符。比如,在我们的日常生活中,如果我们想要开一家店,则要给这个店起一个名字,起的这个“名字”就叫标识符。但是我们店的名字不能叫“警察局”,因为“警察局”这个名字已经被赋予了特殊的含义,而“警察局”就是我们日常生活中的关键字
在我们编写程序的时候,需要大量地为程序、类、变量、方法等取名字,于是就有了 **标识符** 。简单来说, **标识符就是一个名字**
### Java 中的 `53` 个关键字
有一些标识符Java 语言已经赋予了其特殊的含义,只能用于特定的地方,这些特殊的标识符就是 **关键字** 。简单来说,**关键字是被赋予特殊含义的标识**符 。比如,在我们的日常生活中,如果我们想要开一家店,则要给这个店起一个名字,起的这个“名字”就叫标识符。但是我们店的名字不能叫“警察局”,因为“警察局”这个名字已经被赋予了特殊的含义,而“警察局”就是我们日常生活中的关键字。
### Java 语言关键字有哪些?
| 分类 | 关键字 | | | | | | |
| :------------------- | -------- | ---------- | -------- | ------------ | ---------- | --------- | ------ |
| 访问控制 | private | protected | public | | | | |
| :------------------- | -------- |-------- | -------- | -------- | --------| --------| -------- |
| 访问控制 | private | protected | public | | | | |
| 类,方法和变量修饰符 | abstract | class | extends | final | implements | interface | native |
| | new | static | strictfp | synchronized | transient | volatile | enum |
| 程序控制 | break | continue | return | do | while | if | else |
| 程序控制 | break | continue | return | do | while | if | else |
| | for | instanceof | switch | case | default | assert | |
| 错误处理 | try | catch | throw | throws | finally | | |
| 包相关 | import | package | | | | | |
| 基本类型 | boolean | byte | char | double | float | int | long |
| | short | null | true | false | | | |
| 变量引用 | super | this | void | | | | |
| 保留字 | goto | const | | | | | |
| 错误处理 | try | catch | throw | throws | finally | | |
| 包相关 | import | package | | | | | |
| 基本类型 | boolean | byte | char | double | float | int | long |
| | short | | | | | | |
| 变量引用 | super | this | void | | | | |
| 保留字 | goto | const | | | | | |
> Tips所有的关键字都是小写的在 IDE 中会以特殊颜色显示。
>
@ -265,6 +190,10 @@ Java 中的注释有三种:
> - 在类,方法和变量修饰符中,从 JDK8 开始引入了默认方法,可以使用 `default` 关键字来定义一个方法的默认实现。
> - 在访问控制中,如果一个方法前没有任何修饰符,则默认会有一个修饰符 `default`,但是这个修饰符加上了就会报错。
注意 ⚠️:`true`, `false`, 和`null`看起来像关键字但不是关键字。
官方文档https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html 。
### 自增自减运算符
在写代码的过程中,常见的一种情况是需要某个整数类型变量增加 1 或减少 1Java 提供了一种特殊的运算符,用于这种表达式,叫做自增运算符(++)和自减运算符(--)。
@ -626,6 +555,85 @@ public native int hashCode();
更多关于 `hashCode()``equals()` 的内容可以查看:[Java hashCode() 和 equals()的若干问题解答](https://www.cnblogs.com/skywang12345/p/3324958.html)
### 什么是可变长参数?
从 Java5 开始Java 支持定义可变长参数,所谓可变长参数就是允许在调用方法时传入不定长度的参数。就比如下面的这个 `printVariable` 方法就可以接受 0 个或者多个参数。
```java
public static void method1(String... args) {
//......
}
```
另外,可变参数只能作为函数的最后一个参数,但其前面可以有也可以没有任何其他参数。
```java
public static void method2(String arg1, String... args) {
//......
}
```
**遇到方法重载的情况怎么办呢?会优先匹配固定参数还是可变参数的方法呢?**
答案是会优先匹配固定参数的方法,因为固定参数的方法匹配度更高。
我们通过下面这个例子来证明一下。
```java
/**
* 微信搜 JavaGuide 回复"面试突击"即可免费领取个人原创的 Java 面试手册
*
* @author Guide哥
* @date 2021/12/13 16:52
**/
public class VariableLengthArgument {
public static void printVariable(String... args) {
for (String s : args) {
System.out.println(s);
}
}
public static void printVariable(String arg1, String arg2) {
System.out.println(arg1 + arg2);
}
public static void main(String[] args) {
printVariable("a", "b");
printVariable("a", "b", "c", "d");
}
}
```
输出:
```
ab
a
b
c
d
```
另外Java 的可变参数编译后实际会被转换成一个数组,我们看编译后生成的 `class`文件就可以看出来了。
```java
public class VariableLengthArgument {
public static void printVariable(String... args) {
String[] var1 = args;
int var2 = args.length;
for(int var3 = 0; var3 < var2; ++var3) {
String s = var1[var3];
System.out.println(s);
}
}
// ......
}
```
## 基本数据类型
### Java 中的几种基本数据类型了解么?