Java learning road-basic knowledge

Java learning road-basic knowledge

1. Identifier

1. What is an identifier?

Identifier refers to a symbol used to identify an entity, and has different meanings in different application environments. In computer programming languages, identifiers are names used by users in programming. They are used to name variables, constants, functions, statement blocks, etc., to establish the relationship between name and use. Identifiers usually consist of letters and numbers and other characters.

2. Identifier naming rules

In Java programs, there are the following rules for identifier naming. If these rules are not followed, the compiler will report an error:

  1. Only with numbers, letters, _ and * named * (* * ** generally does not use named (in general do not use **Enter row command name ( a general and word does not make use Naming);
  2. Identifier cannot start with a number
  3. Identifiers are strictly case sensitive
  4. Keywords cannot be used as identifiers

3. Identifier naming convention

A good identifier naming convention will make the program easy to understand, and it is essential for a good programmer to develop good naming habits.

The following are good habits for identifier naming:

  1. Identifiers should be named according to their names
  2. Identifiers should follow camel case nomenclature
  3. Capitalize the first letter of the class name and interface name, and capitalize the first letter of each subsequent word
  4. The first letter of the variable name and method name is lowercase, and the first letter of each word after it is uppercase
  5. Constant names are all uppercase

Two, keywords

1. What are keywords?

Java keywords are pre-defined in computer language, identifiers with special meaning, sometimes called reserved words, and variables with special meaning. Java keywords have special meanings to the Java compiler. They are used to represent a data type or the structure of a program. Keywords cannot be used as variable names, method names, class names, package names, and parameters.

2. Common keywords and their uses

Keyword meaning
abstract Indicates that the class or member method has abstract properties
assert Assertions, used for program debugging
boolean One of the basic data types, a keyword that declares the Boolean type
break Jump out a block ahead of time
byte One of the basic data types, byte type
case Used in the switch statement to indicate one of the branches
catch Used in exception handling to catch exceptions
char One of the basic data types, character type
class Declare a class
const Keyword reserved, no specific meaning
continue Back to the beginning of a block
default Default, for example, used in a switch statement, indicates a default branch. Java 8 also acts on declaring the default implementation of interface functions
do Used in do-while loop structure
double One of the basic data types, double-precision floating-point number type
else Used in conditional statements to indicate the branch when the condition is not true
enum enumerate
extends Indicates that one type is a subtype of another type. For class, it can be another class or abstract class; for interface, it can be another interface
final Used to illustrate the final attributes, indicating that a class cannot derive subclasses, or member methods cannot be overridden, or the value of member fields cannot be changed, used to define constants
finally Used to handle exceptions, used to declare a block of statements that will basically be executed
float One of the basic data types, single-precision floating-point number type
for A kind of guide word of cyclic structure
goto Keyword reserved, no specific meaning
if Leading words of conditional sentences
implements Indicates that a class implements a given interface
import Indicates that you want to access the specified class or package
instanceof Used to test whether an object is an instance object of a specified type
int One of the basic data types, integer type
interface interface
long One of the basic data types, long integer type
native Used to declare that a method is implemented by a computer-related language (such as C/C++/FORTRAN language)
new Used to create new instance objects
package package
private An access control method: private mode
protected An access control method: protected mode
public An access control method: shared mode
return Return data from member methods
short One of the basic data types, short integer type
static Indicates that it has static properties
strictfp Used to declare that FP_strict (single-precision or double-precision floating-point numbers) expressions follow [IEEE 754](https://baike.baidu.com/item/IEEE 754) arithmetic specifications
super 表明当前对象的父类型的引用或者父类型的构造方法
switch 分支语句结构的引导词
synchronized 表明一段代码需要同步执行
this 指向当前实例对象的引用
throw 抛出一个异常
throws 声明在当前定义的成员方法中所有需要抛出的异常
transient 声明不用序列化的成员域
try 尝试一个可能抛出异常的程序块
void 声明当前成员方法没有返回值
volatile 表明两个或者多个变量必须同步地发生变化
while 用在循环结构中

三、字面值

1. 什么是字面值?

字面值是指在程序中无需变量保存,可直接表示为一个具体的数字或字符串的值。

字面值大体上可以分为整型字面值、浮点字面值、字符和字符串字面值、特殊字面值。

2. 整型字面值

int a1 = 1;      // 十进制
int a2 = 0b1;    // 二进制
int a3 = 0x1;    // 十六进制
long a4 = 100L;  // 表示long型数字时,后面需要带上 "l"或者"L",一般使用"L"便于区分

3. 浮点型字面值

double b1 = 10.1;  // 小数
double b2 = 1.2E2; // 科学计数法表示
float b3 = 10.1F;  // 表示为float型浮点数时,需要在数字后面加 "f"或者"F"

4. 字符及字符串型字面值

char c1 = 'a';     // 字符型字面值需要用单引号包裹,并且只能为一个字符
String c2 = "abc"; // 字符串型字面值需要用双引号包裹

5. 布尔型字面值

boolean a1 = true;  // 真
boolean a2 = false; // 假

6. 特殊字面值

  1. null:是一种特殊的类型(type),可以将它赋给任何引用类型变量,表示这个变量不引用任何东西。如果一个引用类型变量为null,表示这个变量不可用。
  2. Class literal:它是由类,接口,数组或原始类型的名称或伪类型void组成的表达式,后面紧跟 “.class”。Class literal用于表示类型本身!
System.out.println(String.class);
System.out.println(Integer.class);

out:
class java.lang.String
class java.lang.Integer

7. 数值型字面值中使用下划线

JDK7 以后的版本中,可以在数值型字面值(包括整型字面值和浮点字面值)插入一个或者多个下划线。下划线只能用于分隔数字,不能分隔字符与字符,也不能分隔字符与数字。

int a1 = 123_456;
double a2 = 12_3.4_56;

Guess you like

Origin blog.csdn.net/qq_43580193/article/details/109966693